Skip to content

HTTPClient.getString() returns payload on first call only and returns empty after #4951

Closed
@hreintke

Description

@hreintke

Platform

  • Hardware: Wemos mini
  • Core Version: latest git
  • Development Env: Sloeber eclipse
  • Operating System: Windows

Settings in IDE

  • Module: Wemos D1 mini
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • Flash Frequency: 40Mhz
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 921600

Problem Description

Might be working as intended but took me some time to find out when I was creating a small test program.

When the HTTPClient.GET() has finished, the payload can be retrieved with HTTPClient.getString() returns the payload.
However, when calling multiple times, only the first will return the payload, each subsequent call returns empty string.

#include "LocalDefines.h"
#include <ESP8266WiFi.h>
#include "ESP8266HTTPClient.h"

HTTPClient htpc;

void setup()
{
	Serial.begin(115200);

	Serial.printf("Initiating WiFi\r\n");
	WiFi.mode(WIFI_STA);
	Serial.printf("Wifi mode set\r\n");
    WiFi.begin(ssid,password);
    while (WiFi.status() != WL_CONNECTED) {
	    delay(1000);
	    Serial.println("Connecting to WiFi..");
	 }
	Serial.printf("IP = %s\r\n",WiFi.localIP().toString().c_str());


	htpc.begin("http:/www.google.nl");

	if (htpc.GET() > 0)
	{
		String first = htpc.getString();
		String second = htpc.getString();

		Serial.printf("------------------------------\r\n");
		Serial.printf("First len = %d, txt = %s\r\n",first.length(),first.c_str());
		Serial.printf("------------------------------\r\n");
		Serial.printf("Second len = %d, txt = %s\r\n",second.length(),second.c_str());
	}
}

void loop()
{
}

Results in Serial output.

Initiating WiFi
Wifi mode set
Connecting to WiFi..
Connecting to WiFi..
IP = 10.0.0.211
------------------------------
First len = 219, txt = <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
 <TITLE>301 Moved</TITLE></HEAD><BODY>
                                      <H1>301 Moved</H1>
                                                        The document has moved
                                                                              <A HREF="https://www.google.nl/">here</A>.
</BODY></HTML>

------------------------------
Second len = 0, txt =

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions