ESP32 HTTPClient Library – getString

Home / References / ESP32 Library / HTTPClient

Description

The getString method in the ESP32 HTTPClient Library retrieves the full response body from an HTTP request as a single String object. Ideal for processing text-based server responses like JSON or HTML, this method simplifies data handling for makers building IoT applications or web-connected projects with the ESP32.


Syntax and Usage

The getString method is straightforward to use after initiating an HTTP request (e.g., via GET or POST). Below is the basic syntax:

String payload = http.getString();

This method has only one primary usage:

  • Retrieve Full Response Body: After a successful HTTP request, getString collects the entire server response into a String. It’s best suited for small-to-medium payloads due to memory constraints on the ESP32.

Note: Since it returns the response as a String, ensure your ESP32 has sufficient RAM, especially for large responses, to avoid memory issues.

For practical applications and examples of this method, please consult the “Example Code” section on this page. This section provides comprehensive guidance to help you better understand and apply the method effectively.


Argument(s)

This method does not require any arguments. It operates on the HTTP response already received by the HTTPClient object after a request like http.GET() or http.POST() has been executed.


Return Value

The getString method returns a String containing the full body of the HTTP response. If the request fails or no data is available, it may return an empty string (""). For precise error handling, check the HTTP response code using http.GET() or similar methods beforehand.


Example Codes

Below is an example demonstrating the use of getString in a practical scenario. This aligns with the single usage described in “Syntax and Usage.”

Example: Fetching and Displaying a Response from httpbin.org

This example shows how to use getString to retrieve a simple text response from www.httpbin.org after making a GET request. The ESP32 connects to a Wi-Fi network, sends the request, and prints the response to the Serial Monitor.

Replace YOUR_SSID and YOUR_PASSWORD with your Wi-Fi credentials. Upload the code to your ESP32 using the Arduino IDE, then open the Serial Monitor (115200 baud) to see the response from www.httpbin.org/get.

/*
 * Author: Avant Maker
 * Date: February 24, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's getString to retrieve a 
 * simple text response from www.httpbin.org after making 
 * a GET request. The ESP32 connects to a Wi-Fi network,
 * sends the request, and prints the response
 * to the Serial Monitor.
 *
 * License: MIT 
 * 
 * Code Source: This example code is sourced from the Comprehensive 
 * Guide to the ESP32 Arduino Core Library, accessible on 
 * AvantMaker.com. For additional code examples and in-depth 
 * documentation related to the ESP32 Arduino Core Library, 
 * please visit:
 *
 * https://avantmaker.com/references/esp32-arduino-core-index/
 *
 * AvantMaker.com, your premier destination for all things 
 * DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated 
 * to empowering makers, learners, and enthusiasts with the resources
 * they need to bring their innovative ideas to life.
 */

#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "YOUR_SSID";        // Replace with your Wi-Fi SSID
const char* password = "YOUR_PASSWORD"; // Replace with your Wi-Fi password

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);
    
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nConnected to Wi-Fi");
}

void loop() {
    if (WiFi.status() == WL_CONNECTED) {
        HTTPClient http;
        http.begin("http://www.httpbin.org/get"); // Request URL
        int httpCode = http.GET();               // Send GET request

        if (httpCode > 0) {                      // Check for successful response
            String payload = http.getString();   // Get response body as String
            Serial.println("HTTP Response Code: " + String(httpCode));
            Serial.println("Response: " + payload);
        } else {
            Serial.println("Error on HTTP request: " + String(httpCode));
        }
        
        http.end();                              // Free resources
    } else {
        Serial.println("Wi-Fi disconnected");
    }
    
    delay(10000); // Wait 10 seconds before next request
}
error: Content is protected !!