Obtaining the https response using esp_http_client_perform

antusystem
Posts: 2
Joined: Thu Jan 07, 2021 2:48 am

Obtaining the https response using esp_http_client_perform

Postby antusystem » Sat Jan 09, 2021 8:13 pm

Hi, I'm using the ESP-IDF v.4.2. I'm using the esp_http_client example to make an HTTPS request. Guiding me from the https_with_url function I could establish a connecting with the web, but I want to know the actual content from the response. I cannot see how to do this with the function esp_http_client_perform.

Afterwards, I tried using the base of the http_native_request function, making the changes so I could connect through HTTPS. I manage to do it and also could read the content response from the server to the HTTPS request using esp_http_client_read_response.

So, because esp_http_client_perform is easier to use than all the way with the native path, I'm trying to extract this information but I could not do it. I also try adding a queue in the handler to send this data, but it didn't work, the data arrive incomplete or not even readable.

Hopefully, some of you might know this. Thanks

Note: here you can see the esp_http_client example: https://github.com/espressif/esp-idf/bl ... _example.c



Edit: reading better the esp_http_client and found that they declare a variable for the buffer and if in the config for the handler of the http you pass this variable to .user_data you, after using esp_http_client_perform you can check the response from the server there.

Code: Select all

	
    char buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; 
    esp_http_client_config_t config = {
        .url = "https://postman.org",
        .transport_type = HTTP_TRANSPORT_OVER_SSL,
        .event_handler = _http_event_handler,
        .user_data = buffer,        // Pass address of local buffer to get response
    };
The thing I want to know now is how to print the https request send to the server, for both GET and POST cases.

eliasorggro
Posts: 4
Joined: Wed Jun 08, 2022 8:25 am

Re: Obtaining the https response using esp_http_client_perform

Postby eliasorggro » Sun Jul 17, 2022 7:12 pm

I have the same issue :(
any progress ?

pablomartinez
Posts: 5
Joined: Thu Jul 29, 2021 7:57 am

Re: Obtaining the https response using esp_http_client_perform

Postby pablomartinez » Thu Jul 28, 2022 2:48 pm

You need to capture HTTP_EVENT_ON_DATA event and copy the received data chunks to your user_data buffer.

I defined the event handler:

Code: Select all

esp_err_t client_event_get_handler(esp_http_client_event_handle_t evt)
{
    static char *output_buffer;  // Buffer to store response of http request from event handler
    static int output_len;       // Stores number of bytes read

    switch (evt->event_id)
    {
    case HTTP_EVENT_ON_DATA:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
        /*
         *  Check for chunked encoding is added as the URL for chunked encoding used in this example returns binary data.
         *  However, event handler can also be used in case chunked encoding is used.
         */
        if (!esp_http_client_is_chunked_response(evt->client)) {
            // If user_data buffer is configured, copy the response into the buffer
            if (evt->user_data) {
                memcpy(evt->user_data + output_len, evt->data, evt->data_len);
            } else {
                if (output_buffer == NULL) {
                    output_buffer = (char *) malloc(esp_http_client_get_content_length(evt->client));
                    output_len = 0;
                    if (output_buffer == NULL) {
                        ESP_LOGE(TAG, "Failed to allocate memory for output buffer");
                        return ESP_FAIL;
                    }
                }
                memcpy(output_buffer + output_len, evt->data, evt->data_len);
            }
            output_len += evt->data_len;
        }

        break;
    case HTTP_EVENT_ON_FINISH:
        ESP_LOGD(TAG, "HTTP_EVENT_ON_FINISH");
        if (output_buffer != NULL) {
            // Response is accumulated in output_buffer. Uncomment the below line to print the accumulated response
            // ESP_LOG_BUFFER_HEX(TAG, output_buffer, output_len);
            free(output_buffer);
            output_buffer = NULL;
        }
        output_len = 0;
        break;
    case HTTP_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
        int mbedtls_err = 0;
        esp_err_t err = esp_tls_get_and_clear_last_error(evt->data, &mbedtls_err, NULL);
        if (err != 0) {
            ESP_LOGI(TAG, "Last esp error code: 0x%x", err);
            ESP_LOGI(TAG, "Last mbedtls failure: 0x%x", mbedtls_err);
        }
        if (output_buffer != NULL) {
            free(output_buffer);
            output_buffer = NULL;
        }
        output_len = 0;
        break;
    default:
        break;
    }
    return ESP_OK;
}
And then add your user_data and event_handler to the esp_http_client_config:

Code: Select all

   char responseBuffer[MAX_HTTP_OUTPUT_BUFFER];
 
    esp_http_client_config_t config = {
        .url = url,
        .crt_bundle_attach = esp_crt_bundle_attach,
        .user_data = responseBuffer,
        .event_handler = client_event_get_handler,
    };
After performing esp_http_client_perform(client) successfully, responseBuffer will contain the response.

This worked for me!

Who is online

Users browsing this forum: No registered users and 130 guests