Home / References / ESP32 Library / HTTPClient
Description
The setFollowRedirects
method in the ESP32 HTTPClient Library allows you to control how the HTTP client handles URL redirects. When a server responds with a redirect status code (e.g., 301, 302), this method determines whether the client automatically follows the redirect to the new location or stops at the initial response. This is essential for creating robust web-connected projects, ensuring your ESP32 can seamlessly navigate modern web APIs and services.
Syntax and Usage
The setFollowRedirects
method is straightforward to use. It configures the redirect behavior before making an HTTP request. Below is the basic syntax, followed by the different ways it can be applied:
void setFollowRedirects(followRedirects_t follow)
Here are the usage options:
- Disable Redirect Following: Prevents the client from following redirects, allowing you to manually handle the redirect response (e.g., for custom logic or debugging).
- Enable Strict Redirect Following: Automatically follows redirects, adhering strictly to the HTTP protocol, stopping if the redirect is invalid or exceeds limits.
- Enable Forced Redirect Following: Aggressively follows all redirects, even if they deviate from standard protocol, ensuring the final resource is reached.
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)
The setFollowRedirects
method requires one argument to define its behavior:
- follow (followRedirects_t): An enumeration type specifying the redirect policy. Possible values are:
HTTPC_DISABLE_FOLLOW_REDIRECTS
: Disables automatic redirect following.HTTPC_STRICT_FOLLOW_REDIRECTS
: Enables strict redirect following, respecting HTTP standards.HTTPC_FORCE_FOLLOW_REDIRECTS
: Forces the client to follow all redirects, regardless of protocol deviations.
Return Value
The setFollowRedirects
method does not return a value (void). It modifies the internal configuration of the HTTPClient object, affecting how subsequent HTTP requests handle redirects.
Example Codes
This code demonstrates how to use the ESP32 HTTPClient
library’s setFollowRedirects()
method to automatically follow HTTP redirects. It sends a GET request to a URL that redirects once, and the method ensures the client follows the redirection to retrieve the final response.
To use this code, replace “your_SSID” and “your_PASSWORD” with your WiFi credentials. Upload the code to your ESP32, open the Serial Monitor, and observe the connection process and the response payload after the redirect is followed.
/*
* Author: Avant Maker
* Date: February 23, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's setFollowRedirects
* method to follow HTTP redirects
*
* 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";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
HTTPClient http;
http.setFollowRedirects(HTTPC_FORCE_FOLLOW_REDIRECTS);
http.begin("http://www.httpbin.org/redirect/1"); // URL that redirects once
int httpCode = http.GET();
if (httpCode > 0) {
String payload = http.getString();
Serial.println("Response payload:");
Serial.println(payload);
} else {
Serial.printf("GET request failed with error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void loop() {
// put your main code here, to run repeatedly:
}
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!