ESP32 HTTPClient Library – begin

Home / References / ESP32 Library / HTTPClient

Description

The end method in the ESP32 HTTPClient Library is used to terminate an HTTP connection and free up resources associated with the HTTPClient object. This method should be called after completing an HTTP request (e.g., GET or POST) to ensure that the connection is properly closed and memory is released. It is an essential part of managing network resources efficiently in your ESP32 projects.


Syntax and Usage

The end method is straightforward and does not provide multiple signatures. It is invoked on an HTTPClient object to close the connection. Below is the basic syntax:

http.end();
  • Basic Usage (Without Arguments): Simply call end() on the HTTPClient object after performing an HTTP operation to close the connection and clean up resources.

There are no alternative usages with arguments, as the method is designed to unconditionally terminate the connection.


Argument(s)

This method does not require any arguments. It is a simple void function that operates on the HTTPClient instance itself, closing the underlying network client and resetting internal states without needing additional input.


Return Value

The end method does not return any value. It is a void function, meaning its purpose is solely to perform the cleanup operation without providing feedback. Success or failure of the operation is not explicitly indicated, as it is assumed to always succeed in releasing resources.


Example Codes

Below is an example demonstrating the use of the end method in a typical HTTP GET request scenario. Since there is only one way to use the method (without arguments), a single example is provided.

Example: Performing an HTTP GET Request and Closing the Connection

This example shows how to use the ESP32 to connect to www.example.com, perform an HTTP GET request, and then properly close the connection using the end method. Upload this code to your ESP32, ensuring it is connected to a Wi-Fi network, and open the Serial Monitor at 115200 baud to see the output.

/*
 * Author: Avant Maker
 * Date: February 21, 2025
 * Version: 1.0
 * Description: This example code demonstrates how to 
 * use the ESP32 HTTPClient Library's end method to connect
 * to www.example.com, perform an HTTP GET request, and 
 * then properly close the connection using the end method. 
 * Upload this code to your ESP32, ensuring it is connected
 * to a Wi-Fi network, and open the Serial Monitor at 115200
 * baud to see the output.
 *
 * 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 network credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";

void setup() {
    Serial.begin(115200);
    
    // Connect to Wi-Fi
    WiFi.begin(ssid, password);
    Serial.print("Connecting to Wi-Fi");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nConnected!");

    // Initialize HTTPClient
    HTTPClient http;

    // Begin connection to the example website
    http.begin("http://www.example.com");

    // Send GET request
    int httpCode = http.GET();

    // Check the response
    if (httpCode > 0) {
        String payload = http.getString();
        Serial.println("HTTP Response Code: " + String(httpCode));
        Serial.println("Response: " + payload);
    } else {
        Serial.println("Error on HTTP request: " + String(httpCode));
    }

    // Close the connection
    http.end();
    Serial.println("Connection closed.");
}

void loop() {
    // Nothing to do here
}
error: Content is protected !!