ESP32 HTTPClient Library – clearAllCookies

Home / References / ESP32 Library / HTTPClient

Description

The clearAllCookies method in the ESP32 HTTPClient Library is a utility function designed to remove all stored cookies from the cookie jar associated with an HTTPClient instance. Cookies are often used in web communication to maintain stateful information, such as session IDs or user preferences, between the ESP32 and a server. By calling this method, you can reset the cookie jar, effectively clearing all previously stored cookies and starting fresh. This is particularly useful when you need to terminate a session or ensure no prior cookie data affects subsequent HTTP requests.


Syntax and Usage

The clearAllCookies method is straightforward to use and has only one primary way of invocation. Below is the syntax along with a code snippet demonstrating its usage:

void clearAllCookies();
  • Basic Usage: Call this method on an HTTPClient object to erase all cookies stored in its cookie jar. This is a simple, no-argument call that ensures the cookie jar is emptied, making it ideal for scenarios where you want to reset the HTTP session state.

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 operates directly on the HTTPClient instance’s cookie jar, clearing all stored cookies without needing additional input from the user.


Return Value

The clearAllCookies method does not return a value (i.e., its return type is void). Its purpose is to perform an action—clearing the cookie jar—rather than to provide feedback about the operation. If the cookie jar is empty or not initialized, the method executes without error, making it a safe operation to call at any time.


Example Codes

Below is an example demonstrating how to use the clearAllCookies method in a practical ESP32 project. This example connects to www.httpbin.org, performs an HTTP request, stores cookies (if any), and then clears them.

Example: Clearing Cookies After an HTTP Request

This example shows how to set up an HTTPClient instance, make a GET request to www.httpbin.org, and then clear all cookies from the cookie jar. It assumes the ESP32 is connected to a Wi-Fi network. Replace YOUR_SSID and YOUR_PASSWORD with your Wi-Fi credentials.

/*
 * Author: Avant Maker
 * Date: February 25, 2025
 * Version: 1.0
 * Description: This example demonstrates how to use the 
 * clearAllCookies method to  clear all cookies from the cookie jar.
 *
 * 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>

// Wi-Fi credentials
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);

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

void loop() {
    if (WiFi.status() == WL_CONNECTED) {
        HTTPClient http;

        // Initialize cookie jar (required for cookie management)
        CookieJar cookieJar;
        http.setCookieJar(&cookieJar);

        // Begin HTTP request to a test endpoint
        Serial.println("Making HTTP request...");
        http.begin("http://www.httpbin.org/cookies/set?testcookie=12345");
        int httpCode = http.GET();

        if (httpCode > 0) {
            Serial.println("Request successful. Cookies may have been set.");
        } else {
            Serial.println("Request failed!");
        }

        // Verify cookies are set by making another request
        http.begin("http://www.httpbin.org/cookies");
        httpCode = http.GET();
        if (httpCode > 0) {
            String response = http.getString();
            Serial.println("Response after setting cookies:");
            Serial.println(response);
        }

        // Clear all cookies
        Serial.println("Clearing all cookies...");
        http.clearAllCookies();

        // Verify cookies are cleared by making another request
        http.begin("http://www.httpbin.org/cookies");
        httpCode = http.GET();
        if (httpCode > 0) {
            String response = http.getString();
            Serial.println("Response after clearing cookies:");
            Serial.println(response);
        }

        http.end();
    }

    delay(10000); // Wait 10 seconds before the next loop
}

Explanation: In this code, the ESP32 connects to Wi-Fi and uses the HTTPClient Library to send a GET request to www.httpbin.org/cookies/set, which sets a test cookie. The setCookieJar method links a CookieJar object to the HTTPClient instance to enable cookie storage. After the request, clearAllCookies is called to remove all cookies. A second request to www.httpbin.org/cookies verifies that the cookie jar is empty, as the response will show no cookies. The serial output helps you track the process.

error: Content is protected !!