Unclear instructions on how to use esp_http_client (http_event_handler mandatory?)

roadrunner
Posts: 1
Joined: Tue Feb 25, 2020 7:45 pm

Unclear instructions on how to use esp_http_client (http_event_handler mandatory?)

Postby roadrunner » Tue Feb 25, 2020 8:09 pm

Hello,

I've looked up the esp_http_client examples (https://github.com/espressif/esp-idf/tr ... ttp_client).
But there's no single example showing on how to get the response body of a request.

My understanding for now is, that you have to register a "_http_event_handler" and work on the "HTTP_EVENT_ON_DATA" event to receive the (maybe chunked) body of a response.

Would it also be possible to work without said handler? I don't like to maintain a "global" variable that holds the current response body.

I've also tried the "esp_http_client_read" function, which works fine for non chunked responses. But I couldn't figure out how to handle chunked responses correctly when using "esp_http_client_read" with "esp_http_client_fetch_headers" etc.

Would be great if there was a function like "esp_http_client_perform" that also gives u the response body.

So basically my question is: Is the "_http_event_handler" mandatory when working with (chunked) responses?

Thanks.

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

Re: Unclear instructions on how to use esp_http_client (http_event_handler mandatory?)

Postby boarchuz » Wed Feb 26, 2020 3:32 am

You don't need the event_handler. You can use esp_http_client_read after perform (I'm 99% sure, at least. I agree the docs/examples should be clearer here.). You can use it for chunked just the same.

You can provide a pointer in the client config that will be passed to the event handler so you don't need to mess about in the global space.

ESP_Shubham
Posts: 8
Joined: Mon Oct 07, 2019 6:09 am

Re: Unclear instructions on how to use esp_http_client (http_event_handler mandatory?)

Postby ESP_Shubham » Fri Feb 28, 2020 1:53 pm

Hello,

Thanks for reporting this issue with the documentation.

There are two ways to get response body, using event handler and using esp_http_client_read.

While using event handler, you can receive data using HTTP_EVENT_ON_DATA event as shown in https://github.com/espressif/esp-idf/bl ... mple.c#L59.

The other option is to use esp_http_client_read. In this case, esp_http_perform should not be used. Application is responsible connect to the server and send http_request.

I'll try improving the documentation for the same and add an example to read response using esp_http_client_read.

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

Re: Unclear instructions on how to use esp_http_client (http_event_handler mandatory?)

Postby boarchuz » Fri Feb 28, 2020 6:12 pm

ESP_Shubham wrote:
Fri Feb 28, 2020 1:53 pm
The other option is to use esp_http_client_read. In this case, esp_http_perform should not be used. Application is responsible connect to the server and send http_request.
I think there should be a dead simple GET request example using esp_http_perform without the need for an event handler.

Even after your reply here I'm not sure how to get the response from it. I understand how the event_handler works but I'm convinced that esp_http_perform was (and should be) designed without the need for one.

Won't esp_http_client_read work (though this requires another large allocation to copy the response)? Can't the client handle->response->buffer be accessed directly?

ESP_Shubham
Posts: 8
Joined: Mon Oct 07, 2019 6:09 am

Re: Unclear instructions on how to use esp_http_client (http_event_handler mandatory?)

Postby ESP_Shubham » Mon Mar 09, 2020 6:50 am

We will update the esp_http_client example to demonstrate use of esp_http_client_read(). Along with it, will also add a buffer to accumulate response body from event handler.

User avatar
parthbhat13
Posts: 8
Joined: Wed Oct 07, 2020 2:07 pm

Re: Unclear instructions on how to use esp_http_client (http_event_handler mandatory?)

Postby parthbhat13 » Tue Jul 02, 2024 9:16 am

Hi,

is there any update on this scenario? since even i am facing the similar issue where i would want to not go inside of the eventHandle and read the data, instead would want to use the esp_http_client_read.

the instructions or the example still happens to be unclear. so far the only thing i can see in the instructions is the following.

You must never call this function simultaneously from two places using the same client handle. Let the function return first before invoking it another time. If you want parallel transfers, you must use several esp_http_client_handle_t. This function include esp_http_client_open -> esp_http_client_write -> esp_http_client_fetch_headers -> esp_http_client_read (and option) esp_http_client_close.

but even that does not explain how exactly shall we implement the example?
does it mean that instead of calling the esp_http_client_perform, we have to instead do the following?

1. esp_http_client_open
2. esp_http_client_write
3. esp_http_client_fetch_headers
4. esp_http_client_read
5. esp_http_client_close

now it makes me wonder, why is there no simple example for this? as well, following the above 5 steps would solve the issue? and what would happen to the esp_http_client_cleanup? as the esp_http_client_close is indeed called in the esp_http_client_cleanup, but then there are even more other functions called as well.

how would this work out? any ideas?

Who is online

Users browsing this forum: Google [Bot] and 114 guests