ESP32 HTTPClient Library – setURL

Home / References / ESP32 Library / HTTPClient

Description

The setURL() method in the ESP32 HTTPClient Library allows you to change the URL of an existing HTTPClient instance after it has been initialized with begin(). This method is particularly useful in IoT applications where you need to dynamically update the target URL without reinitializing the entire client, making it a flexible tool for HTTP operations in STEM projects.


Syntax and Usage

The setURL() method has a single usage pattern that requires a URL argument. Below is the syntax and how to use it:

bool HTTPClient::setURL(const String& url)

Usage with URL Argument: Call this method on an initialized HTTPClient object to set or update the target URL. It takes a single argument (the new URL as a String) and returns a boolean indicating success or failure. This is ideal for scenarios where the endpoint changes during runtime.


Argument(s)

url (type: String): The new URL to set for the HTTP client. This should be a valid URL (e.g., “http://www.example.com/”). It replaces the previously set URL, allowing the client to redirect its requests accordingly.


Return Value

The setURL() method returns a bool value:

  • true: Indicates that the URL was successfully set, and the HTTP client is ready to use the new URL.
  • false: Indicates that the URL could not be set (e.g., due to an invalid URL format or internal client error).

Example Codes

Below is an example demonstrating the use of the setURL() method. This example initializes an HTTPClient, connects to an initial URL, then updates it to www.example.com using setURL(), and performs an HTTP GET request.

Example: Dynamically Changing the URL with setURL()

This code demonstrates how to use the ESP32 HTTPClient Library’s setURL() method to dynamically change the target URL of an HTTP request. It first connects to Wi-Fi, then starts with an initial URL and updates it using the setURL() method before performing a GET request.

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 observe the connection process, URL update, and HTTP GET request results.

/*
 * Author: Avant Maker
 * Date: February 21, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's setURL method to 
 * set URL www.example.com, and performs a GET request. 
 * Ensure your ESP32 is connected to a Wi-Fi network before running 
 * this code. 
 *
 * 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(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");
}

void loop() {
    HTTPClient http;

    Serial.println("[HTTP] Beginning with initial URL...");
    http.begin("http://initial.example.com/"); // Initial URL

    // Change the URL dynamically
    Serial.println("[HTTP] Setting new URL...");
    if (http.setURL("http://www.example.com/")) {
        Serial.println("[HTTP] URL successfully updated to www.example.com");
    } else {
        Serial.println("[HTTP] Failed to update URL");
        http.end();
        return;
    }

    // Perform GET request with the new URL
    int httpCode = http.GET();
    if (httpCode > 0) {
        Serial.println("[HTTP] GET request successful, code: " + String(httpCode));
        String payload = http.getString();
        Serial.println("Response: " + payload);
    } else {
        Serial.println("[HTTP] GET request failed, error: " + String(httpCode));
    }

    http.end(); // Close the connection
    delay(5000); // Wait 5 seconds before next loop
}
        
error: Content is protected !!