I need to connect my esp32 to a website.
The protocol used is of the HTTPS type.
However I'm encountering problems on the connection to the website. In particular, to obtain the OK response from the website (code 200), I need to send an header.
One example I found doesn't use headers (https_request). Searching docs I see that headers are included through the esp_http_client_set_header() function.
So I think I need this code:
Code: Untitled.c Select all
#define MAX_HTTP_RECV_BUFFER 512
#define MAX_HTTP_OUTPUT_BUFFER 2048
/* Constants that aren't configurable in menuconfig */
#define WEB_PORT "443"
#define WEB_URL "https://put_a_website.com"
static const char *TAG = "example";
static void https(void)
{
esp_http_client_config_t config = {
.url = WEB_URL,
.transport_type = HTTP_TRANSPORT_OVER_SSL,
.cert_pem = esp_crt_bundle_attach,
};
esp_http_client_handle_t client = esp_http_client_init(&config);
esp_http_client_set_url(client, WEB_URL);
esp_http_client_set_header(client, "header1",
"header2");
esp_err_t err = esp_http_client_perform(client);
if (err == ESP_OK)
{
ESP_LOGI(TAG, "Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
}
else
{
ESP_LOGE(TAG, "Error perform http request %s", esp_err_to_name(err));
}
esp_http_client_cleanup(client);
}
void app_main(void)
{
ESP_ERROR_CHECK( nvs_flash_init() );
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
https();
}
Code: Select all
initialization of 'const char *' from incompatible pointer type 'esp_err_t (*)(void *)' {aka 'int (*)(void *)'}
I saw this on one of the examples, I can't understand why I have this problem (I enabled the certificate bundle from menuconfig).
If I remove the line above, I run the code but this time I obtain the following output:
Code: Select all
W (4726) wifi:<ba-add>idx:1 (ifx:0, 30:b5:c2:fe:36:60), tid:4, ssn:0, winSize:64
E (4756) esp-tls-mbedtls: No server verification option set in esp_tls_cfg_t structure. Check esp_tls API reference
E (4756) esp-tls-mbedtls: Failed to set client configurations
E (4766) esp-tls: create_ssl_handle failed
E (4766) esp-tls: Failed to open new connection
E (4776) TRANS_SSL: Failed to open a new connection
E (4776) HTTP_CLIENT: Connection failed, sock < 0
E (4786) example: Error perform http request ESP_ERR_HTTP_CONNECT
