Home / References / ESP32 Library / WiFiClientSecure
Description
The readString
method of the ESP32’s WiFiClient
library reads all available characters from the server and returns them as a String object. This is particularly useful for handling server responses where the length of the incoming data is unknown or when you want to process the entire response at once.
Syntax and Usage
The readString
method is used as follows:
String response = client.readString();
Here, client
is an instance of WiFiClient
. The method does not take any arguments and returns the data as a String.
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.
Return Value
The readString
method returns a String containing all the characters read from the server. If no data is available, it returns an empty String.
Example Codes
Below is an example demonstrating how to use the readString
method to read the entire response from a server:
Example: Reading Server Response
This example connects to www.httpbin.org
and sends a simple HTTP GET request. It then reads the server’s response using the readString
method and prints it to the Serial Monitor.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example demonstrates how to use
* ESP32 WiFiClient Library's readString method to
* read the entire response from a server.
*
* 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>
const char* ssid = "your-ssid"; // Replace with your WiFi SSID
const char* password = "your-password"; // Replace with your WiFi password
void setup() {
Serial.begin(115200);
delay(1000);
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
}
void loop() {
WiFiClient client;
const char* host = "www.httpbin.org";
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("Connection failed");
return;
}
// Send HTTP GET request
client.print(String("GET /get HTTP/1.1\r\n") +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
// Wait for server response
while (client.connected() && !client.available()) {
delay(1);
}
// Read the response
String response = client.readString();
Serial.println("Server response:");
Serial.println(response);
// Close the connection
client.stop();
// Wait before the next request
delay(10000);
}
In this example:
- The ESP32 connects to the specified Wi-Fi network.
- It establishes a connection to
www.httpbin.org
on port 80. - An HTTP GET request is sent to the server.
- The response from the server is read using
client.readString()
and printed to the Serial Monitor. - The connection is then closed, and the device waits for 10 seconds before repeating the process.
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFiClient Library
- Connection
- Send Data
- Receive Data
- Config
- Status
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!