Hello,
On ESP8266 or ESP32 I wish to make asynchroneous http GET request, but I have difficulties to find which librairy to use and how to use.
(For synchroneous I can with ESP8266HTTPClient, but I want asynch)
when you see that the WiFiClient available() is not null it means the answer has arrived.
You'll need to parse the headers and then receive the body.
The headers are separated from the body by an empty line (the last 4 bytes should be "\r\n\r\n")
you need to look into the headers:
➜ if the client instead sends a Content-Length, you must use that to know how many bytes to expect in the body section.
➜ If the client sends a header with Transfer-Encoding: Chunked, you will need to parse the answer using the chunked transfer encoding syntax. When the client is using this approach, you can detect the end of the body by a chunk with a length of 0.
(this parsing code probably exists in some of the libraries).
don't use while() otherwise you are making it synchronous
don't mix client.connected() and client.available()
you let the loop spin and it's very much like what's discussed in Serial Input Basics except that your stream is not coming from Serial but from the client and the buffer is much larger.
if after a request client.connected() is false, it means the client is gone. Just handle that.
ifclient.available() is not null, then it has accumulated (part of) the answer. In many cases it will hold the full HTTP answer, but to know what's coming you'll need to parse the content.
you have a small state machine:
read each line that is in the incoming buffer. check for Content-Length or Transfer-Encoding and make a note of the information they provide to adjust your state machine.
wait until you get the empty line (the last 4 bytes you received should be "\r\n\r\n" )
use the Content-Length and/or Transfer-Encoding to receive the body
just let the loop loops, deal with whatever comes in the buffer.
Thanks I will later take the time to read,
but I think you overlook the fact that there could be miscalleneous requests and also to several servers.
Example