Home / References / ESP32 Library / HTTPClient
Description
The hasHeader
method in the ESP32 HTTPClient Library checks whether a specific header exists in the server’s response to an HTTP request and has a non-empty value. This is particularly useful when you need to verify the presence of a header before attempting to process its value, ensuring your code handles responses robustly and efficiently.
Syntax and Usage
The hasHeader
method is straightforward to use. It requires a single argument specifying the header name to check. Below is the syntax:
bool hasHeader(const char* name);
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)
- name (const char*): The name of the header to check (e.g.,
"Content-Type"
or"Server"
). This is a case-insensitive string specifying the header you want to verify. It must be a null-terminated C-string.
Return Value
The hasHeader
method returns a boolean value:
- true: The specified header exists in the response and has a non-empty value.
- false: The specified header either does not exist or has an empty value.
Example Codes
Below is an example demonstrating how to use the hasHeader
method in a practical scenario. This example connects to www.httpbin.org
, a useful testing service, to retrieve response headers and check for a specific one.
Example: Checking for the “Content-Type” Header
This code sends an HTTP GET request to http://www.httpbin.org/get
, collects specific headers, and checks if the "Content-Type"
header is present in the response. It’s a great starting point for understanding how to work with response headers in your ESP32 projects.
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 demonstrates how to
* use the ESP32 HTTPClient Library's hasHeader to check
* if the "Content-Type" header is present in the response.
*
* 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 WiFi SSID
const char* password = "your-PASSWORD"; // Replace with your WiFi password
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
HTTPClient http;
http.begin("http://www.httpbin.org/get"); // Connect to the test server
// Define headers to collect
const char* headerKeys[] = {"Content-Type", "Server"};
http.collectHeaders(headerKeys, 2);
int httpCode = http.GET(); // Send the GET request
if (httpCode > 0) { // Check for a successful request
Serial.printf("HTTP request succeeded with code: %d\n", httpCode);
// Check if the "Content-Type" header exists
if (http.hasHeader("Content-Type")) {
Serial.println("Content-Type header is present!");
Serial.print("Value: ");
Serial.println(http.header("Content-Type")); // Print the header value
} else {
Serial.println("Content-Type header is not present or empty.");
}
} else {
Serial.printf("HTTP request failed with error code: %d\n", httpCode);
}
http.end(); // Free resources
}
void loop() {
// Nothing to do here
}
How to Use This Example: Replace your-SSID
and your-PASSWORD
with your WiFi credentials. Upload the code to your ESP32 using the Arduino IDE. Open the Serial Monitor (115200 baud) to see the output. The code will connect to WiFi, send a GET request, and check for the "Content-Type"
header, printing its value if present.
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!