Home / References / ESP32 Library / HTTPClient
Description
The headerName
method in the ESP32 HTTPClient Library retrieves the name of an HTTP header at a specified index from the server’s response. This method is invaluable when you’ve collected headers using collectHeaders
and need to iterate through or identify them dynamically. Whether you’re debugging a response or building a project that reacts to server metadata, this method empowers makers to unlock deeper insights from HTTP interactions.
Syntax and Usage
The headerName
method has a single usage, requiring an index as an argument. Here’s how you can use it:
- With Argument: Pass an integer index to retrieve the name of a specific header collected from the HTTP response. This is useful after setting up header collection with
collectHeaders
, allowing you to access header names programmatically.
String headerName(int index);
Note: To use this method effectively, you must first call ESP32 HTTPClient Library’s collectHeaders first 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 headerName
method requires one argument:
- index: An integer (
int
) representing the position of the header in the list of collected headers. The index starts at 0 and must be less than the total number of headers collected (accessible viaheaders
method).
Return Value
The headerName
method returns a String
containing the name of the header at the specified index. If the index is invalid (e.g., out of range), it returns an empty string (""
). This allows you to safely retrieve and process header names from the server’s response.
Example Codes
Below is an example showcasing the headerName
method in action. This example connects to www.httpbin.org
and demonstrates how to collect and list header names from the response.
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.
Example: Retrieving and Printing Header Names
This code connects an ESP32 to Wi-Fi, sends an HTTP GET request to www.httpbin.org/get
, collects specific headers, and then uses headerName
to print the names of all collected headers to the Serial Monitor. It’s a great way to explore how headers are structured in a response—perfect for learning and prototyping!
/*
* Author: Avant Maker
* Date: February 24, 2025
* Version: 1.0
* Description: This example code demonstrates how to
* use the ESP32 HTTPClient Library's headerName method to print
* the names of all collected headers to the Serial Monitor.
* 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("\nConnected to Wi-Fi!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Define headers to collect
const char* headerKeys[] = {"Content-Type", "Server"};
const size_t headerKeysCount = 2;
// Configure HTTPClient to collect headers
http.collectHeaders(headerKeys, headerKeysCount);
// Start the HTTP request
http.begin("http://www.httpbin.org/get");
int httpCode = http.GET();
if (httpCode > 0) {
// Request successful, retrieve and print header names
Serial.printf("HTTP Response Code: %d\n", httpCode);
int headerCount = http.headers(); // Get the number of collected headers
Serial.printf("Number of headers collected: %d\n", headerCount);
for (int i = 0; i < headerCount; i++) {
String headerNameStr = http.headerName(i);
String headerValue = http.header(i);
Serial.printf("Header %d - Name: %s, Value: %s\n", i, headerNameStr.c_str(), headerValue.c_str());
}
} else {
Serial.println("Error on HTTP request");
}
http.end(); // Free resources
}
delay(10000); // Wait 10 seconds before the next request
}
Notes: Replace your-SSID
and your-PASSWORD
with your Wi-Fi credentials. Open the Serial Monitor at 115200 baud to see the header names and their values. This example collects Content-Type
and Server
headers, but you can modify the headerKeys
array to collect others!
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!