I know it's bad to use Strings due to the memory problems that can occur. I've tried to remove all instances of strings from my code. But I'm not sure about this line:

http.getString().toCharArray(ipAddressBuffer, lengthOfIPAddress);  

Is calling toCharArray() enough to prevent memory problems? Or is there a string still being created on the heap?

If it's still created on the heap, how can I modify the code so it only deals with cstrings?

Here's my code:

void setIPAddress(char* ipAddressBuffer){
  HTTPClient http;
  http.begin("http://bot.whatismyipaddress.com/"); //Only allowed to call once per 5 mins.
  http.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36");
  int httpCode = http.GET();
  if (httpCode > 0) {    
    http.getString().toCharArray(ipAddressBuffer, lengthOfIPAddress);    
  else {
    Serial.println("Error on HTTP request");

I'm using the HTTPClient.h library btw.

  • BTW: Which HTTPClient library? I know one which allows for HttpClient objects, not for HTTPClient May 21, 2020 at 9:07

2 Answers 2


The return value of getString allocates and the deletes a char array in heap for a temporary String object.

For ESP8266HTTPClient and ESP32 HTTPClient library you can use writeToStream or getStream()to read the response without using String class with getString(), where stream is WiFiClient& stream = client.getStream();

getStream, returns the underlying WiFiClient. You can read the data with classic while (stream.available()) { char c = stream.read; ... }

writeToStream copies the data to a stream. My StreamLib library has a stream wrapper over a char array. It is called CStringBuilder.


http.getString() creates a temporary Arduino String object and fills text into dynamic memory on the heap. Calling the method .toCharArraycopies this text to a char array, wherever you defined it. Immediately after that, the temporary String gets out of scope and the text on the heap is deleted again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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