Brotchu
Brotchu

Reputation: 135

Making HTTPS requests from ESP32

I am making a post request from my ESP32 S2 Kaluga kit. I have tested the HTTP request while running a server program in my LAN. I am using esp_http_client_handle_t and esp_http_client_config_t from esp_http_client.h to do this.

Now, I have a HTTPS api setup in AWS API gateway. I get following error with https now:

E (148961) esp-tls-mbedtls: No server verification option set in esp_tls_cfg_t structure. Check esp_tls API reference
E (148961) esp-tls-mbedtls: Failed to set client configurations, returned [0x8017] (ESP_ERR_MBEDTLS_SSL_SETUP_FAILED)
E (148971) esp-tls: create_ssl_handle failed
E (148981) esp-tls: Failed to open new connection
E (148981) TRANSPORT_BASE: Failed to open a new connection
E (148991) HTTP_CLIENT: Connection failed, sock < 0

How can I solve this? Thank you

Edit: Following is the code I use I create a http client for post request:

esp_err_t client_event_get_handler(esp_http_client_event_handle_t evt)
{
    switch (evt->event_id)
    {
    case HTTP_EVENT_ON_DATA:
        printf("HTTP GET EVENT DATA: %s", (char *)evt->data);
        break;
    
    default:
        break;
    }
    return ESP_OK;
}

static void post_rest_function( char *payload , int len)
{
    esp_http_client_config_t config_post = {
        .url = SERVER_URL,
        .method = HTTP_METHOD_POST,
        .event_handler = client_event_get_handler,
        .auth_type = HTTP_AUTH_TYPE_NONE,
        .transport_type = HTTP_TRANSPORT_OVER_TCP
    };

    esp_http_client_handle_t client = esp_http_client_init(&config_post);
    
    esp_http_client_set_post_field(client, payload, len);
    esp_http_client_set_header(client, "Content-Type", "image/jpeg");

    esp_http_client_perform(client);
    esp_http_client_cleanup(client);
}

and I use it in main with an image payload:

void app_main(){
....
post_rest_function( (char *)pic->buf, pic->len);
....
}

Upvotes: 4

Views: 8134

Answers (2)

Nikhil Sekhar
Nikhil Sekhar

Reputation: 61

Additionally, you may choose to include the certificates to make sure that your transfer is safe (valid server).

You can obtain the root SSL certificate of your host like so watch through till 56 minute mark for a complete explanation.

OR you may use the included certificate bundle that espressif provides in the IDF framework, for that:

In your code include #include "esp_crt_bundle.h" and in your client_config_t add these:

.transport_type = HTTP_TRANSPORT_OVER_SSL,  //Specify transport type
.crt_bundle_attach = esp_crt_bundle_attach, //Attach the certificate bundle 

after which the process remains quite the same.

The video I linked above is quite helpful, I recommend you watch the whole thing :)

Upvotes: 6

rafael ache
rafael ache

Reputation: 36

You need certificate to make https requests. In case you dont want to implement this, just edit your sdkconfig "Allow potentially insecure options" -> true

"Skip server certificate verification by default" -> true

Careful, this is unsafe.

Upvotes: 2

Related Questions