boblemaire / asyncHTTPrequest

asynchronous HTTP for ESP using ESPasyncTCP. Works like XMLHTTPrequest in JS.
GNU General Public License v3.0
64 stars 31 forks source link

Issue when assigning result to a string #33

Closed scleach closed 3 years ago

scleach commented 3 years ago

Hi I've used the sample and it works, however I cannot assign the result to a string. It just prints blank in the serial monitor. if(readyState == 4){ Serial.println(request->responseText()); String responseResult = request->responseText(); Serial.println(responseResult); Could you explain how to do this? Stephen Leach

scleach commented 3 years ago

using .str(); is the answer String responseResult = request->responseText(); Serial.println(responseResult.c_str());

boblemaire commented 3 years ago

Serial.println() should work OK with a String argument. In any event responseText() returns the data available at the time of the call and deletes it from the buffer. In your case you are doing it at readyState 4 so it should be all of the data, but consider if you are getting a long response and taking the data with responseText() in an onData callback at readyState 3. You want each call to pickup where the last left off.

So is it possible that you had something like:

String responseResult = request->responseText(); Serial.println(request->responseText());

In that case responseResult would have the response, and the second call to responseText() would return a null string.

I would suggest the the "solution" code:

String responseResult = request->responseText(); Serial.println(responseResult.c_str());

Would work just as well with:

String responseResult = request->responseText(); Serial.println(responseResult);

It really doesn't have anything to do with asyncHTTPrequest. It's work is done in the first line. The printf is overloaded and should work fine with either a String or char* argument.