ArduinoHttpClient sporadic blank response

I am looking for some advice on how to troubleshoot my issue. I have a Nano IOT calling a remote web service. Most of the time clientHTTP.responseBody() produces the response I'm expecting, then after 50 web service calls (this ranges quite a bit though) response doesn't have any content, and the status code is always 200 even with a blank response. I do not see any issues at the web service and have an Adafruit Feather calling during the same intervals that doesn't have this issue. I thought it was memory-related because I do not see available memory decreasing. I detect x number of blank responses, restart the device and I start receiving responses again. I am using a String to read the contents of the clientHTTP.responseBody() and have read that Strings cause memory issues. I see the following output from freeMemory, Free RAM = 22595

            clientHTTP.beginRequest();
            clientHTTP.post(GetHelpURL);
            clientHTTP.sendHeader("Content-Type", "application/x-www-form-urlencoded");
            clientHTTP.sendHeader("Content-Length", 0);
            clientHTTP.sendHeader("X-Custom-Header", "custom-header-value");
            clientHTTP.beginBody();
            clientHTTP.print("");
            clientHTTP.endRequest();

            int wsStatusCode = clientHTTP.responseStatusCode();
            String wsResponse = clientHTTP.responseBody();

            Serial.print(F("response: "));
            Serial.println(wsResponse);

Please show ALL your code, the snippets are of little use for understanding problems

I would print out the response headers to see if the data is in some unexpected format or there was a non-HTTP error.

I think this, between:
int wsStatusCode = clientHTTP.responseStatusCode();
and:
String wsResponse = clientHTTP.responseBody();

   while (clientHTTP.headerAvailable())
    {
      Serial.print("header: ");
      Serial.print(clientHTTP.readHeaderName());
      Serial.print(": ");
      Serial.println(clientHTTP.readHeaderValue());
    }

John you indirectly pointed me in the right direction, although I took a very roundabout way getting there. I didn't read your post thoroughly and put your code and the following code below the String wsResponse command. What I noticed was when I would get an empty response in my string the command below would get the proper response, seems that I have a timing issue (sometimes). Seems that the Arduino is trying to read the response before it is ready, it's just odd that it works fine for hours, stops working and then repeats the pattern after a reboot.

          int JsonPtr = 0;
          char JsonC;
          while (clientHTTP.available() && JsonPtr < JsonBufferSize) {
            JsonC = clientHTTP.read();
            Serial.print(JsonC);
            if (JsonC == '\n') // End of line
              break;
            if (JsonC != '\r') // Ignore CRs
            {
              json[JsonPtr++] = JsonC;
            }
          }

Following up on this request for my solution, I think my web service was the issue. I had it setup as an async POST and was trying to read the response. My code would work for hours and then stop reading the response but would read the response after restarting the Arduino. I changed my web service into two, GET and POST. I read the response body from my GET and just the status code from the POST and it working as expected with no issues.

Hopefully this helps someone else

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