0

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

3

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.

4
  • Is there a way I can adapt my code so that it will still work? – Tom Schoehuijs May 16 '18 at 18:49
  • isn't it obvious? read into variable and then print the value of the variable. remove the redundant println – Juraj May 16 '18 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 May 16 '18 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. – Juraj May 16 '18 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.