ESP32 HTTPClient Library – resetCookieJar

Home / References / ESP32 Library / HTTPClient

Description

The resetCookieJar method is a utility function in the ESP32 HTTPClient Library designed to manage cookie storage. It resets the cookie jar by disassociating it from the HTTPClient instance, effectively disabling cookie persistence for subsequent requests. This is useful when you want to clear the existing cookie jar reference without deleting the cookies themselves, allowing you to start fresh or switch to a different cookie management strategy.


Syntax and Usage

The resetCookieJar method is straightforward to use and has only one form of invocation. Below is the syntax:

http.resetCookieJar();

This method is called on an HTTPClient object (e.g., http) and does not accept any arguments. It simply sets the internal cookie jar pointer to nullptr, stopping the use of the previously set cookie jar without affecting the cookies stored in it.

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 solely on the HTTPClient instance it is called upon, making it a simple and direct way to reset cookie management.


Return Value

The resetCookieJar method does not return any value (i.e., its return type is void). Its purpose is to modify the state of the HTTPClient object by resetting the cookie jar reference, and it performs this action silently without providing feedback through a return value.


Example Codes

Below is an example demonstrating the use of the resetCookieJar method. Since there is only one way to use this method (as outlined in “Syntax and Usage”), a single example is provided. This example shows how to set up a cookie jar, use it, and then reset it.

Example: Using and Resetting a Cookie Jar with HTTPClient

This example connects an ESP32 to Wi-Fi, makes an HTTP request to www.httpbin.org (a useful testing site), sets up a cookie jar, and then resets it. The code demonstrates the lifecycle of cookie management and how resetCookieJar fits into it. Replace YOUR_SSID and YOUR_PASSWORD with your Wi-Fi credentials.

/*
 * Author: Avant Maker
 * Date: February 24, 2025
 * Version: 1.0
 * Description: This example demonstrates how to use the 
 * resetCookieJar method to reset cookies with the ESP32 
 * HTTPClient Library. The ESP32 connects to a WiFi network, 
 * makes an HTTP request to www.httpbin.org (a useful testing site),
 * sets up a cookie jar, and then resets it. 
 *
 * 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);
    delay(1000);

    // 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 to Wi-Fi");
}

void loop() {
    if (WiFi.status() == WL_CONNECTED) {
        HTTPClient http;
        CookieJar cookieJar; // Create a cookie jar instance

        // Set the cookie jar for the HTTPClient instance
        http.setCookieJar(&cookieJar);
        Serial.println("Cookie jar set.");

        // Make an HTTP GET request to httpbin.org
        http.begin("http://www.httpbin.org/cookies/set?test_cookie=example_value");
        int httpCode = http.GET();

        if (httpCode > 0) {
            Serial.println("Request successful. Cookies may have been stored.");
            String payload = http.getString();
            Serial.println("Response: " + payload);
        } else {
            Serial.println("Request failed with code: " + String(httpCode));
        }

        // 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);
        }  

        // Reset the cookie jar
        http.resetCookieJar();
        Serial.println("Cookie jar reset. No further cookies will be stored.");

        // Make another request to demonstrate the reset
        http.begin("http://www.httpbin.org/cookies");
        httpCode = http.GET();

        if (httpCode > 0) {
            Serial.println("Second request successful. No cookies should be sent.");
            String payload = http.getString();
            Serial.println("Response: " + payload);
        } else {
            Serial.println("Second request failed with code: " + String(httpCode));
        }

        http.end(); // Free resources
    }

    delay(30000); // Wait 30 seconds before repeating
}

In this example, the ESP32 first connects to Wi-Fi, then uses the HTTPClient library to send a request that sets a cookie via www.httpbin.org/cookies/set. The cookie is managed by the CookieJar instance. After the first request, resetCookieJar is called, disassociating the cookie jar. A second request to www.httpbin.org/cookies shows that no cookies are sent, confirming the reset. Check the Serial Monitor at 115200 baud to see the output.

error: Content is protected !!