That is example from library:
void simple_WIFI_download(void) {
WiFiClient client;
HTTPClient http; // must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...)
debug.print("[HTTP] begin...\n");
// configure server and url
http.begin(client, "http://homeblinker.000webhostapp.com/files/esp-config/esp-config.json");
// http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html");
debug.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
debug.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK) {
// get length of document (is -1 when Server sends no Content-Length header)
int len = http.getSize();
// create buffer for read
uint8_t buff[64] = { 0 };
// or "by hand"
// get tcp stream
WiFiClient* stream = &client;
// read all data from server
while (http.connected() && (len > 0 || len == -1)) {
// read up to 128 byte
int c = stream->readBytes(buff, std::min((size_t)len, sizeof(buff)));
debug.printf("readBytes: %d\n", c);
if (!c) {
debug.println("read timeout");
debug.println("Output:");
debug.println(http.getString());
}
// write it to debug
debug.write(buff, c);
if (len > 0) { len -= c; }
}
debug.println();
debug.print("[HTTP] connection closed or file end.\n");
}
}
else {
debug.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
delay(60000);
}
And its output:
[HTTP10] begin...
[HTTP] GET...
[HTTP] GET... code: 200
readBytes: 0
read timeout
Output:
{
"stroke1": ["12", "02883345"],
"stroke2": ["14", "02883345"],
"stroke3": "test",
"stroke4": "",
"stroke5": ["0","1","2","3","4","5"],
"stroke6": ["","","2","3","4","5"]
}
[HTTP] connection closed or file end.
As we can see esp for some reason receive data using 'getString()' function but not the stream. Why if wait for stream reading it becomes infinitely?