# Use String class in AsyncWebServerRequest

**URL:** <https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184>\
**Category:** Programming\
**Created:** [May 28, 2024, 1:34pm UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184 "2024-05-28T13:34:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelheiml82](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/michaelheiml82/32/1074946_2.png) [@michaelheiml82](https://forum.arduino.cc/u/michaelheiml82)\
**Post date:** [May 28, 2024, 1:34pm UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184/1 "2024-05-28T13:34:43Z")

</div>

Beginner question:

I've read a lot about avoid String class due to memory fragmentation, so I tried to get rid of all references and usages. But for the code below I do not know how to do that. Could someone please point me in the right direction or is it safe to use the code like it is?

```arduino
JsonDocument responseData;
String response;

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
  {
    responseData["Value"] = someValue; //someValue increases over time, like i++
    //String response; //do not use String class;
    serializeJson(responseData, response); //Use declared string
    request->send(200, "application/json", response);
  }
  );

```

I've come across SafeString Library but failed to use it here instead (too little knowledge).

I've declared response variable because I assume that if the variable is declared inside the {response} it will be created again and again for each response instead of created only once? Or is it ok to create "String response;" with each web request?

And does the increasing value (i) also lead to issues with the String class? This whole "avoid String class" thing confuses me a lot.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 28, 2024, 4:27pm UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184/2 "2024-05-28T16:27:22Z")

</div>

> [@michaelheiml82](#):
>
> This whole "avoid String class" thing confuses me a lot.

seems you are using ArduinoJSON. It uses dynamic memory allocation all over, so Strings should not worry you more than necessary (assuming you are on a ESP32).

that being said, if you want to only send someValue which is an integer, you don't need a JSON document for that...

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [May 29, 2024, 5:42am UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184/3 "2024-05-29T05:42:00Z")

</div>

> [@michaelheiml82](#):
>
> Could someone please point me in the right direction

on an esp just use .reserve() if needed and you are fine.

> **[reserve() - Arduino Reference](https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/reserve/)**
>
> The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 29, 2024, 6:33am UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184/4 "2024-05-29T06:33:55Z")

</div>

for this specific case (assuming someValue is an integer) I would not use ArduinoJSON nor the String class.

this would use up 30 bytes on the stack for the buffer

```arduino
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    char response[30]; // make sure you have enough space
    // build the JSON answer
    snprintf(response, sizeof response, "{\"Value\": %d}", someValue); // see https://cplusplus.com/reference/cstdio/snprintf/
    request->send(200, "application/json", response);
  }
);

```

you could use snprintf() returned value to ensure the whole JSON fitted in the buffer.

* * *

alternatively you can do it without any buffer at all in your code by printing to the underlying stream.

```arduino
void handleRequest(AsyncWebServerRequest *request) {
  AsyncResponseStream *response = request->beginResponseStream("application/json");
  response->setCode(200); // returned code for the response to say all went fine

  // Print directly to the response stream
  response->print("{\"value\": ");
  response->print(someValue);
  response->print("}");

  request->send(response);
}

server.on("/", HTTP_GET, handleRequest);

```

---

<div class="post-metadata">

**Author:** ![michaelheiml82](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/michaelheiml82/32/1074946_2.png) [@michaelheiml82](https://forum.arduino.cc/u/michaelheiml82)\
**Post date:** [May 29, 2024, 7:31am UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184/5 "2024-05-29T07:31:30Z")

</div>

ArduinoJSON - yes.

Its more than just this one value which I added as example.

I basically have 2 "String" situations:

1. deserializing a config.txt file from the LittleFS file system with a JSON. Contains around 20 variables.
2. the example above, giving me some basic stats via a simple web request (like how often the device restarted, wifi disconnected..)

I use ESP8266 ESP-12F

ESP.getFreeHeap() returns around 40.000 constantly - is that an OK value or should I be worried? (Unfortunately I have not checked getFreeHeap before I removed most of String references)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 29, 2024, 8:24am UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184/6 "2024-05-29T08:24:16Z")

</div>

> [@michaelheiml82](#):
>
> ESP.getFreeHeap() returns around 40.000 constantly - is that an OK value or should I be worried

that's plenty for 20 variables and their (short) labels.

---

<div class="post-metadata">

**Author:** ![michaelheiml82](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/michaelheiml82/32/1074946_2.png) [@michaelheiml82](https://forum.arduino.cc/u/michaelheiml82)\
**Post date:** [May 29, 2024, 8:30am UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184/7 "2024-05-29T08:30:20Z")

</div>

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 25, 2024, 8:30am UTC](https://forum.arduino.cc/t/use-string-class-in-asyncwebserverrequest/1265184/8 "2024-11-25T08:30:56Z")

</div>

Closed on November 25, 2024, 8:30am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
