HTTP Client: What happens if the server response is too large?

TobiasUhmann
Posts: 9
Joined: Mon Nov 27, 2023 8:11 am

HTTP Client: What happens if the server response is too large?

Postby TobiasUhmann » Wed Apr 17, 2024 11:41 am

Hi there,

I am using the HTTP client to do HTTP requests. One request should return a JSON that might be multiple megabytes large. I want to handle the case that it is larger than the available RAM. How do I do that properly?

The HTTP client docs (https://docs.espressif.com/projects/esp ... lient.html) do not specify much about the data_len attribute of the esp_http_client_event struct. Since I couldn't find a KConfig setting for some static buffer size for the HTTP client, I assume the memory for the event payload is allocated on the heap. What happens if malloc fails? Will I get an HTTP_EVENT_ERROR?

Also, I cannot change the API endpoint to use chunked encoding.

Thanks in advance!

TobiasUhmann
Posts: 9
Joined: Mon Nov 27, 2023 8:11 am

Re: HTTP Client: What happens if the server response is too large?

Postby TobiasUhmann » Wed Apr 17, 2024 11:52 am

Ok.. once again, after having searched a while, I find the obvious answer just after having written the post here. Hoping that it might nonetheless spare someone else some time, I do not delete my question and post the simple answer:

The esp_http_client_config_t has a buffer_size attribute (that defaults to DEFAULT_HTTP_BUF_SIZE = 512). Larger responses lead to multiple HTTP_EVENT_ON_DATA. I thought there was only one HTTP_EVENT_ON_DATA for non-chunked HTTP requests, which is not the case.

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: HTTP Client: What happens if the server response is too large?

Postby MicroController » Wed Apr 17, 2024 12:37 pm

TobiasUhmann wrote:
Wed Apr 17, 2024 11:41 am
How do I do that properly?
1) esp_http_client_open(...)
2) repeat esp_http_client_read(...) until all data read.

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: HTTP Client: What happens if the server response is too large?

Postby chegewara » Wed Apr 24, 2024 12:19 am

TobiasUhmann wrote:
Wed Apr 17, 2024 11:52 am
Ok.. once again, after having searched a while, I find the obvious answer just after having written the post here. Hoping that it might nonetheless spare someone else some time, I do not delete my question and post the simple answer:

The esp_http_client_config_t has a buffer_size attribute (that defaults to DEFAULT_HTTP_BUF_SIZE = 512). Larger responses lead to multiple HTTP_EVENT_ON_DATA. I thought there was only one HTTP_EVENT_ON_DATA for non-chunked HTTP requests, which is not the case.
Yes, thats how it works.
Remember that wifi MTU is around 1500, so you have "to read in chunks" anyway, just it has nothing to do with http chunked encoding.
When you receive multiple on data events you have to concatenate it somehow, either in ram or in file on flash or sd card.

Hello1024
Posts: 4
Joined: Thu Apr 25, 2024 6:39 pm

Re: HTTP Client: What happens if the server response is too large?

Postby Hello1024 » Thu Apr 25, 2024 7:41 pm

Worth noting that it is also possible to parse json in chunks, keeping the state of the decoder between chunks.

So it's easily possible to process a 1MB JSON file downloded over HTTP with the available RAM, as long as the fields you need from the json are themselves small.

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: HTTP Client: What happens if the server response is too large?

Postby chegewara » Fri Apr 26, 2024 5:47 am

Hello1024 wrote:
Thu Apr 25, 2024 7:41 pm
Worth noting that it is also possible to parse json in chunks, keeping the state of the decoder between chunks.

So it's easily possible to process a 1MB JSON file downloded over HTTP with the available RAM, as long as the fields you need from the json are themselves small.
Can you share how to parse big (not completed) JSON?

Hello1024
Posts: 4
Joined: Thu Apr 25, 2024 6:39 pm

Re: HTTP Client: What happens if the server response is too large?

Postby Hello1024 » Fri Apr 26, 2024 11:31 am

Looks like this project does it:

https://github.com/kazuho/picojson/blob ... reaming.cc

Your code will have to be pretty customized to pull out the values needed sadly. That example can already parse json bigger than RAM as long as it is in the form of:

Code: Select all

[
{"x": "hello", "y": "test", "other_ignored": {whatever other data....},
{"x": "hello", "y": "test", "other_ignored": {whatever other data....},
{"x": "hello", "y": "test", "other_ignored": {whatever other data....},
]
At any point in time, only one 'line' of the json above is loaded in memory (although it doesn't rely on linebreaks).

You'll need to implement your own istream_iterator using blocking calls to esp_http_client_read(), and if you don't want it to block, I suggest using a new FreeRTOS task.

A pretty big task I'm afraid.

MicroController
Posts: 1725
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: HTTP Client: What happens if the server response is too large?

Postby MicroController » Sat Apr 27, 2024 1:26 pm

https://github.com/skeeto/pdjson
sound promising.

https://github.com/squix78/json-streaming-parser
looks like it could be made to work without Arduino libs.

https://github.com/GOB52/gob_json
seems to be available for both Arduino and non-Arduino.

Who is online

Users browsing this forum: No registered users and 473 guests