http client read response headers

rfleming
Posts: 62
Joined: Tue Oct 09, 2018 12:30 am

http client read response headers

Postby rfleming » Thu Feb 11, 2021 1:40 am

Hi all,

I currently have my esp32-Wrover-E setup as a http client. The client connects to my server and sends and receives JSON formatted data. In this situation I use the headers to determine the content type and since I will be receving roughly 2k data, I required to use the esp_http_client_open/esp_http_client_write method instead of esp_http_client_perform.

Since using this method, the function esp_http_client_fetch_headers is actually used for getting data length from the header and not the headers itself.

Looking through the API there doesn't appear to be any method for getting these headers in response from the server. I am doing this synchronously (so is_async=false). Even so I implemented the callback function to check if it comes through there, but it too doesn't show any headers with its HTTP_EVENT_ON_HEADER event_id.

Finally I went looking through the esp_http_client.c file and looked through the structure for the esp_http_client_handle_t and noted that it 'could' be accessed via client->response->headers. Though this always appears to be null and never populated - hence no api for it.

So my question is, does this functionality current exist in esp-idf v4.1? Else I may try to write my own function extending the esp_http_client_fetch_headers to include the headers I required.

Stephan
Posts: 12
Joined: Tue Aug 27, 2019 2:42 am

Re: http client read response headers

Postby Stephan » Wed Jun 23, 2021 8:47 am

How did you solve it? I came across the same issue. I need a cookie that is returned from a server, but the standard functions don't hand it over.

rfleming
Posts: 62
Joined: Tue Oct 09, 2018 12:30 am

Re: http client read response headers

Postby rfleming » Fri Jun 25, 2021 11:39 pm

Turns out headers were all parsed via the event loop instead of the main functions. So when received, I just stored them in an array and processed them in the main function. I am unsure about cookies though.

User avatar
fasani
Posts: 197
Joined: Wed Jan 30, 2019 12:00 pm
Location: Barcelona
Contact:

Re: http client read response headers

Postby fasani » Sun Jun 27, 2021 8:10 am

Hi rfleming,
Can you expand a bit more on how to read the response headers?

I’m trying to do the same to read the Mesh lamp nodes from mesh_info call and still could not get it to work.
I see the four headers coming in ON_HEADER event, but evt length and data do not seem to be populated.
epdiy collaborator | http://fasani.de Fan of Espressif MCUs and electronic design

Stephan
Posts: 12
Joined: Tue Aug 27, 2019 2:42 am

Re: http client read response headers

Postby Stephan » Sun Jun 27, 2021 8:20 am

I solved the issue with the callback functions of the HTTP parser. They read everything and can make it explicit. I described this here:
viewtopic.php?f=13&t=21598
I am, however, not sure if this is possible only with IDF 4.2 and later.

User avatar
fasani
Posts: 197
Joined: Wed Jan 30, 2019 12:00 pm
Location: Barcelona
Contact:

Re: http client read response headers

Postby fasani » Sun Jun 27, 2021 7:52 pm

Thanks Stephan,
it's actually in the http_client example and I just missed it.
Here an example to query a root mesh lamp and ask for the child nodes.
epdiy collaborator | http://fasani.de Fan of Espressif MCUs and electronic design

Who is online

Users browsing this forum: No registered users and 239 guests