Home / References / ESP32 Library / HTTPClient
Description
The setCookieJar
method in the ESP32 HTTPClient Library enables cookie management for HTTP requests. By associating a CookieJar
object with an HTTP client instance, this method allows you to store and manage cookies received from a server, facilitating session persistence and stateful interactions with web services. It’s a powerful tool for projects requiring authentication or tracking across multiple requests.
Syntax and Usage
The setCookieJar
method is straightforward to use. Below is the basic syntax, followed by its practical application:
void setCookieJar(CookieJar *cookieJar);
This method has a single usage scenario:
- Using with a CookieJar pointer: Assigns a
CookieJar
object to the HTTP client to enable cookie storage and retrieval during HTTP transactions.
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)
CookieJar *cookieJar
: A pointer to aCookieJar
object, which serves as the container for storing cookies. This object must be instantiated separately before being passed to the method. If set tonullptr
, cookie management is disabled.
Return Value
The setCookieJar
method does not return a value (i.e., its return type is void
). It modifies the internal state of the HTTPClient
instance by linking it to the specified CookieJar
object for subsequent HTTP requests.
Example Codes
Below is an example demonstrating how to use the setCookieJar
method in a practical scenario. This example connects an ESP32 to www.httpbin.org
, a popular testing service, to showcase cookie handling.
Example: Setting up a CookieJar for an HTTP Request
This example demonstrates how to use the setCookieJar
method to manage cookies with the ESP32 HTTPClient Library. The ESP32 connects to a WiFi network, initializes an HTTPClient
and a CookieJar
, and links them using setCookieJar
. It then sends a GET request to www.httpbin.org/cookies/set?AvantMaker=HelloESP32
to set a cookie. After the request, the code retrieves and displays the stored cookies (e.g., ‘AvantMaker=HelloESP32
‘) via the Serial Monitor by iterating over the CookieJar
and printing each cookie’s name and value. Replace your-SSID
and your-PASSWORD
with your WiFi credentials to test it.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example demonstrates how to use the
* setCookieJar method to manage cookies with the ESP32
* HTTPClient Library. The ESP32 connects to a WiFi network,
* initializes an HTTPClient and a CookieJar, and links
* them using setCookieJar. It then sends a GET request
* to www.httpbin.org/cookies/set?AvantMaker=HelloESP32
* to set a cookie.
* After the request, the code retrieves and displays
* the stored cookies (e.g., 'AvantMaker=HelloESP32') via
* Serial Monitor by iterating over the CookieJar
* and printing each cookie’s name and value.
* Replace your-SSID and your-PASSWORD with
* your WiFi credentials to test 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>
// WiFi 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 WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
// Initialize HTTPClient and CookieJar
HTTPClient http;
CookieJar cookieJar;
// Associate the CookieJar with the HTTPClient instance
http.setCookieJar(&cookieJar);
// Begin the HTTP request to set a cookie
http.begin("http://www.httpbin.org/cookies/set?AvantMaker=HelloESP32");
int httpCode = http.GET();
// Check the response
if (httpCode > 0) {
Serial.println("Request successful");
String payload = http.getString();
Serial.println("Response: " + payload);
// Display the stored cookies from the CookieJar
Serial.println("Stored Cookies in ESP32:");
for (size_t i = 0; i < cookieJar.size(); i++) {
Serial.print("Cookie ");
Serial.print(i + 1);
Serial.print(": ");
Serial.print(cookieJar[i].name);
Serial.print("=");
Serial.println(cookieJar[i].value);
}
// 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);
}
} else {
Serial.println("Error on HTTP request");
}
// Clean up
http.end();
}
void loop() {
// Nothing to do here
}
ESP32 Library Index
- ESP32 WiFi Library
- ESP32 WiFiClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- 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!