For some reason I can't seem to pass a simple string from one variable to another. Below is my code:
HTTPClient http; // Declare an object of class HTTPClient
http.begin(address); // Specify request destination
int httpCode = http.GET(); // Send the request
Serial.println("String: " + http.getString());
String prp = http.getString();
Serial.println("temp string: " + prp);
It prints out:
String: < ! DOCTYPE HTML> 26
temp string:
Why doesnt my string get correctly passed on to my temporary string variable?
Tom
1 Answer 1
The getString() method calls writeToStream
which is documented write all message body / payload to Stream
. It writes all the data of the http response to he output and doesn't store them internally. The next call has nothing to read because the first call to getString() put everything out.
-
Is there a way I can adapt my code so that it will still work?Tom Schoehuijs– Tom Schoehuijs2018年05月16日 18:49:19 +00:00Commented May 16, 2018 at 18:49
-
isn't it obvious? read into variable and then print the value of the variable. remove the redundant println2018年05月16日 19:00:07 +00:00Commented May 16, 2018 at 19:00
-
Isn't that what I do? The first println works fine, it's the second one that's the problem!Tom Schoehuijs– Tom Schoehuijs2018年05月16日 19:04:57 +00:00Commented May 16, 2018 at 19:04
-
you throw away the String used in first println. but the response was only in that String. call to getString put the received bytes into that String.2018年05月16日 19:15:39 +00:00Commented May 16, 2018 at 19:15