Home / References / ESP32 Library / HTTPClient
Description
The setReuse
method in the ESP32 HTTPClient Library allows you to enable or disable the keep-alive feature for HTTP connections. By keeping the TCP connection open after a request, you can reuse it for subsequent requests to the same server, reducing overhead and improving performance in your projects. This is particularly useful for applications requiring frequent communication with a server, such as IoT devices or data loggers.
Syntax and Usage
Here’s how to use the setReuse
method in your ESP32 sketches. It’s straightforward and takes a single boolean argument to control connection reuse.
void setReuse(bool reuse);
- Enabling Connection Reuse: Pass
true
to keep the TCP connection alive for multiple requests, ideal for repeated server interactions. - Disabling Connection Reuse: Pass
false
to close the connection after each request, useful when you need a fresh connection every time.
Argument(s)
- reuse (bool): A boolean value that determines whether the TCP connection should be kept open (
true
) or closed (false
) after a request. By default, this is set totrue
in the HTTPClient class.
Return Value
The setReuse
method does not return any value (it is a void
function). It simply configures the HTTPClient object to either maintain or terminate the connection based on the provided argument.
Example Codes
Below are practical examples demonstrating the two ways to use setReuse
. Each example connects to www.httpbin.org
, a handy testing server, to showcase the method in action.
Enabling Connection Reuse
This code demonstrates how to use the ESP32 HTTPClient Library’s setReuse()
method to enable reusing the same HTTP connection for multiple requests. By calling setReuse(true)
, the connection remains open after the first request, improving efficiency when making subsequent requests to the same server.
To use this code, replace “your-SSID” and “your-PASSWORD” with your Wi-Fi credentials. Upload the code to your ESP32 board and open the Serial Monitor to see the responses from two GET requests made over a single reused connection.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's setReuse to enable
* the keep-alive feature for HTTP connections.
*
* 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 WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.setReuse(true); // Enable connection reuse
http.begin("http://www.httpbin.org/get");
// First request
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("First Request Response: " + payload);
} else {
Serial.println("Error on first request");
}
// Second request reusing the connection
httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Second Request Response: " + payload);
} else {
Serial.println("Error on second request");
}
http.end(); // Close the connection manually
}
delay(5000); // Wait before next loop
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 HTTPClient Library
- Connection
- Request Methods
- Request Config
- Response
- Cookie
- 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!