Home / References / ESP32 Library / HTTPClient
Description
The getLocation
method in the ESP32 HTTPClient Library retrieves the URL of a redirect location provided by an HTTP server in its response headers. This is particularly useful when dealing with HTTP redirects (e.g., 301 or 302 status codes), allowing your ESP32 project to follow or analyze the redirection path. Whether you’re building a web scraper or handling dynamic server responses, this method empowers you to adapt to changing endpoints seamlessly.
Syntax and Usage
The getLocation
method is straightforward and has a single way of being invoked. Below is the syntax along with a code snippet to demonstrate its usage:
String location = http.getLocation();
- Basic Usage: Call
getLocation()
on anHTTPClient
object after sending a request and receiving a response. It retrieves the redirect URL (if present) from the “Location” header of the HTTP response. This is typically used when the server responds with a redirect status code, and you’ve enabled redirect following or want to inspect the redirect location manually.
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 simply accesses the redirect location stored internally by the HTTPClient
object after processing the server’s response headers.
Return Value
The getLocation
method returns a String
containing the URL specified in the “Location” header of the HTTP response. If no redirect location is present (e.g., the response isn’t a redirect), it returns an empty String
. This allows you to easily check and utilize the redirect URL in your project logic.
Example Codes
Below is an example demonstrating the use of getLocation
in a practical scenario. This corresponds to the basic usage outlined above.
Example: Checking for Redirects with HTTPClient
This example connects an ESP32 to a Wi-Fi network, sends an HTTP GET request to a test endpoint on www.httpbin.org
that triggers a redirect, and retrieves the redirect location using getLocation
. Upload this code to your ESP32, open the Serial Monitor at 115200 baud, and observe the output to see the redirect URL.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example connects an ESP32 to a
* Wi-Fi network, sends an HTTP GET request to a
* test endpoint on www.httpbin.org that triggers
* a redirect, and retrieves the redirect location
* using getLocation.
*
* 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>
// Replace with your Wi-Fi credentials
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(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Use httpbin.org's redirect endpoint
http.begin("http://www.httpbin.org/redirect/1");
// Enable redirect following (optional, for demonstration)
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("HTTP Response Code: %d\n", httpCode);
// Get the redirect location (if any)
String redirectLocation = http.getLocation();
if (redirectLocation.length() > 0) {
Serial.println("Redirect Location: " + redirectLocation);
} else {
Serial.println("No redirect location found.");
}
} else {
Serial.printf("HTTP GET failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
}
void loop() {
}
Explanation: The code initializes a Wi-Fi connection and sends a GET request to www.httpbin.org/redirect/1
, which responds with a 302 redirect. The setFollowRedirects
method is used here to automatically follow the redirect, but getLocation
still retrieves the redirect URL from the initial response’s “Location” header. The redirect location is printed to the Serial Monitor, helping you understand where the server intended to send the request.
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 AsyncUDP Librarry
- 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!