Home / References / ESP32 Library / HTTPClient
Description
The setRedirectLimit
method in the ESP32 HTTPClient Library allows you to define the maximum number of redirects the HTTP client will follow during a request. When a server responds with a redirect status (e.g., 301 or 302), the client can automatically follow the new location, up to the limit you set with this method. This is particularly useful for handling web requests where multiple redirects might occur, ensuring your ESP32 project remains efficient and avoids infinite redirect loops.
Syntax and Usage
The setRedirectLimit
method is straightforward to use, requiring a single argument to specify the redirect limit. Below is how you can integrate it into your code:
http.setRedirectLimit(limit);
This method is typically called after initializing the HTTPClient object and before making a request, in conjunction with the setFollowRedirects
method, which enables redirect following.
- Using with a specific limit: Set a custom number of redirects (e.g., 5) to control how many times the client follows redirects before stopping.
There is only one way to use this method, as it always requires an argument to define the limit.
For more detailed information and examples of the syntax and usage 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)
- limit (uint16_t): An unsigned 16-bit integer that specifies the maximum number of redirects the HTTP client will follow. For example, setting this to 5 means the client will follow up to 5 redirects before halting. The default value in the library is 10 if not explicitly set.
Return Value
The setRedirectLimit
method does not return a value. It modifies the internal state of the HTTPClient object to enforce the specified redirect limit during subsequent HTTP requests.
Example Codes
Below is an example demonstrating how to use the setRedirectLimit
method in a practical ESP32 project. This example connects to www.httpbin.org
, a website designed for testing HTTP requests, which supports redirect testing.
Example: Setting a Custom Redirect Limit
This code configures the ESP32 to follow up to 3 redirects when making an HTTP GET request to a redirect endpoint on www.httpbin.org
. It demonstrates how to use setRedirectLimit
alongside setFollowRedirects
to manage redirect behavior.
In this example, the /redirect/4
endpoint on www.httpbin.org
triggers 4 redirects. By setting the redirect limit to 3, the client stops after the third redirect, resulting in an incomplete request (likely a 302 response). Adjust the limit to 5 or higher to follow all redirects and reach the final destination. This showcases how setRedirectLimit
gives you control over redirect handling.
/*
* Author: Avant Maker
* Date: February 23, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's setRedirectLimit method.
* In this example, the /redirect/4 endpoint on www.httpbin.org
* triggers 4 redirects. By setting the redirect limit to 3,
* the client stops after the third redirect, resulting in
* an incomplete request (likely a 302 response).
* Adjust the limit to 5 or higher to follow all redirects
* and reach the final destination. This showcases how
* setRedirectLimit gives you control over redirect handling.
*
* 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_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize HTTPClient
HTTPClient http;
// Configure HTTP client
http.begin("http://www.httpbin.org/redirect/4"); // This endpoint redirects 4 times
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS); // Enable strict redirect following
http.setRedirectLimit(3); // Limit to 3 redirects
// Make the GET request
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Response code: " + String(httpCode));
Serial.println("Payload: " + payload);
} else {
Serial.println("Error on HTTP request: " + String(httpCode));
}
http.end(); // Free resources
}
void loop() {
// Nothing to do here
}
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!