Empty payload on http request esp8266

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?

we can't see it. the code uses stream and the output has data

No, we see output from 'getString()' function. Without it esp wait for answer infinitely.

your http object (HTTPClient) has already consumed all the data associated with the GET request so there is nothing for stream (WiFiClient) to read.

.getString() pulls that data out of the http object.

Yes. I call 'getString' only to show that data exists. But the stream read nothing.
Output looks like that:

[HTTP] begin...
[HTTP] GET...
[HTTP] GET... code: 200
readBytes: 0
read timeout
readBytes: 0
read timeout
readBytes: 0
read timeout
readBytes: 0
read timeout
...

@blh64 I suspect that too, but I looked into the source code and I don't see where it happens.
and the example for Stream read is exactly what @dmitryvovk has

Thanks for reading.
The problem is that bugs was occurred in ESP8266 library starts after 3.01 version.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.