Home / References / ESP32 Library / HTTPClient
Description
The header
method in the ESP32 HTTPClient Library retrieves the value of a specific HTTP response header received from a server. This method is invaluable for makers and enthusiasts working on IoT projects, as it allows you to access critical metadata—such as content type, server information, or custom headers—returned in an HTTP response. By mastering this method, you can enhance your ESP32 projects with dynamic server interaction and data processing.
Syntax and Usage
The header
method has two distinct overloads, offering flexibility in how you retrieve header information after an HTTP request. Below are the available usages:
- Retrieve Header by Name: Fetches the value of a specific header by providing its name as a string. This is useful when you know the exact header you need, such as “Content-Type” or “Server”.
String header(const char* name);
- Retrieve Header by Index: Returns the value of a header at a specific index in the collected headers list. This is handy when iterating through all collected headers or when the header name is unknown but its position is predictable.
String header(size_t i);
Note: To use this method effectively, you must first call ESP32 HTTPClient Library’s collectHeaders to specify which headers to capture before making the HTTP request.
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)
The header
method accepts the following arguments, depending on the overload used:
- name (const char*, for first overload): The name of the HTTP header to retrieve (e.g., “Content-Type”). This is case-insensitive.
- i (size_t, for second overload): The index of the header in the list of collected headers, starting from 0. The maximum valid index is determined by the number of headers specified in
collectHeaders
.
Return Value
The header
method returns a String
containing the value of the requested header. If the header is not found (in the name-based overload) or the index is out of bounds (in the index-based overload), an empty String
is returned. This allows you to safely handle cases where the server does not provide the expected header.
Example Codes
Below are example codes demonstrating both ways to use the header
method. These examples connect to www.httpbin.org
, a reliable testing service, to retrieve and display response headers.
Example 1: Retrieve Header by Name
This example shows how to fetch the “Content-Type” header after making a GET request. It’s perfect for beginners wanting to inspect specific server response details.
Note: To use this method effectively, you must first call ESP32 HTTPClient Library’s collectHeaders to specify which headers to capture before making the HTTP request..
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example code demostrates how to use
* ESP32 HTTPClient Library's header method to fetch the
* "Content-Type" header and "Server" header after making
* a GET request. NOTE: To use this method effectively,
* you must first call collectHeaders to specify which
* headers to capture before making the HTTP request.
*
* 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 SSID
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://www.httpbin.org/get");
// Specify headers to collect
const char* headerKeys[] = {"Content-Type", "Server"};
http.collectHeaders(headerKeys, 2);
int httpCode = http.GET();
if (httpCode > 0) {
// Retrieve header by name (Content-Type)
String contentType = http.header("Content-Type");
Serial.println("Response Headers:");
Serial.print("Content-Type: ");
Serial.println(contentType);
// Retrieve header by name (Server)
String server = http.header("Server");
Serial.println("Response Headers:");
Serial.print("Server: ");
Serial.println(server);
} else {
Serial.println("Error on HTTP request");
}
http.end();
}
delay(10000); // Wait 10 seconds before next request
}
Example 2: Retrieve Header by Index
This example demonstrates iterating through collected headers using their indices. It’s ideal for advanced users exploring all available response headers dynamically.
Note: To use this method effectively, you must first call ESP32 HTTPClient Library’s collectHeaders to specify which headers to capture before making the HTTP request.
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example code demostrates how to use
* ESP32 HTTPClient Library's header method to fetch the
* headers after making a GET request.
* NOTE: To use this method effectively,
* you must first call collectHeaders to specify which
* headers to capture before making the HTTP request.
*
* 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(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin("http://www.httpbin.org/get");
// Specify headers to collect
const char* headerKeys[] = {"Content-Type", "Server", "Date"};
http.collectHeaders(headerKeys, 3);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.println("Response Headers:");
// Iterate through headers by index
for (int i = 0; i < http.headers(); i++) {
String headerValue = http.header(i);
String headerName = http.headerName(i);
Serial.print("Header ");
Serial.print(i);
Serial.print(" [");
Serial.print(headerName);
Serial.print("]: ");
Serial.println(headerValue);
}
} else {
Serial.println("Error on HTTP request");
}
http.end();
}
delay(10000); // Wait 10 seconds before next request
}
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!