Issue with http_rest_with_url function

TeeVee
Posts: 11
Joined: Tue Sep 12, 2017 2:00 pm

Issue with http_rest_with_url function

Postby TeeVee » Wed May 20, 2020 8:49 am

For a e-Paper display collecting data from a home automation server via REST we are using the basic examples from esp-idf repo in https://github.com/espressif/esp-idf/bl ... _example.c

Instead of fixed URLs from the example we pass the URLs we want to acquire via char variable *URL:

Code: Select all

static void http_rest_with_url(char *URL)
{
    char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
    esp_http_client_config_t config = {
        .url = URL,
        .event_handler = _http_event_handler,
        .user_data = local_response_buffer,        // Pass address of local buffer to get response
    };
    esp_http_client_handle_t client = esp_http_client_init(&config);

    // GET
    esp_err_t err = esp_http_client_perform(client);
    if (err == ESP_OK) {
        ESP_LOGI(TAG, "HTTP GET Status = %d, content_length = %d",
                esp_http_client_get_status_code(client),
                esp_http_client_get_content_length(client));
    } else {
        ESP_LOGE(TAG, "HTTP GET request failed: %s", esp_err_to_name(err));
    }
    ESP_LOG_BUFFER_HEX(TAG, local_response_buffer, strlen(local_response_buffer));
    ....
 
We only need the GET call so we deleted out all other calls.

Now the issue: When calling the function the first time we see the right response in the local_response_buffer and with the ESP_LOG_BUFFER_HEX macro.
However with second and all subsequent calls no log output is visible. Seems the local_response_buffer cannot be
initialized a second time although it's a local variable?
When changing the statement:

Code: Select all

char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0};
to:

Code: Select all

char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER];
we do define the variable with every call of the function but do not set all contents to "0"
Then we get again the right log with the first call but now every subsequent call appends the contents at the end of the existing
buffer. The buffer contents is correct now but grows longer with every call. THis means with 3 calls you get 3 REST responses in the buffer instead of ony by one.

It's obvious that something goes wrong with initializing local variable local_response_buffer but have no clue what?

We are using Eclipse IDE latest version with latest Eclipse plugin. Compilation works without issues

boarchuz
Posts: 566
Joined: Tue Aug 21, 2018 5:28 am

Re: Issue with http_rest_with_url function

Postby boarchuz » Wed May 20, 2020 3:02 pm

output_len is only being reset in the handler if the buffer is on the heap.
You could move the "output_len = 0;" lines in each of _FINISH and _DISCONNECTED so it's always reset.

TeeVee
Posts: 11
Joined: Tue Sep 12, 2017 2:00 pm

Re: Issue with http_rest_with_url function

Postby TeeVee » Fri May 22, 2020 7:35 am

Great and thanx a lot, indeed setting output_len to 0 unconditionally fixed the issue.

However heap size is reduced with every call to "http_rest_with_url" - is there a way to free heap whe3n leaving the event handler?

boarchuz
Posts: 566
Joined: Tue Aug 21, 2018 5:28 am

Re: Issue with http_rest_with_url function

Postby boarchuz » Fri May 22, 2020 8:58 am

Is it possible you're initialising a new http client (esp_http_client_init) for every request without a esp_http_client_cleanup?
You can either create the esp_http_client_handle_t once and reuse the same one for every request, or create a new one every time and ensure its memory is freed afterwards with esp_http_client_cleanup every time.

You can remove all of the output_buffer stuff in the handler (assuming you're not using it as you're passing the pointer to your local_response_buffer).

TeeVee
Posts: 11
Joined: Tue Sep 12, 2017 2:00 pm

Re: Issue with http_rest_with_url function

Postby TeeVee » Wed May 27, 2020 8:12 am

Thanx for that input however I am not that experienced that I could setup from scratch - is there any example program I could clone for my needs?

Who is online

Users browsing this forum: No registered users and 263 guests