ESP8266 Arduino: Getting HTTP Response headers

In this post we are going to check how to obtain the headers of the response of an HTTP request, using the Arduino core on the ESP8266. The tests of this tutorial were performed using a DFRobot’s ESP8266 FireBeetle board.


Introduction

In this post we are going to check how to obtain the headers of the response of an HTTP request, using the Arduino core on the ESP8266.

We are going to test this for an HTTP GET request made against a testing REST API that we have used in many previous tutorials.

If you need an introduction on how to perform HTTP GET requests from the ESP8266, please check this previous tutorial.

The tests of this tutorial were performed using a DFRobot’s ESP8266 FireBeetle board.

 

The code

First, we need to include the libraries needed for our code. We will need the ESP8266WiFi.h library to be able to connect the ESP8266 to a WiFi network, and the ESP8266HTTPClient.h to be able to make HTTP requests.

Since we are going to connect our device to a WiFi network, we will also need to have the network credentials. Thus, we will store both the network name (SSID) and password in two global variables, so they are easy to change.

Additionally, we will need to specify an array with the names of the headers we want to obtain from the response to the HTTP request. These are going to be used later. We will assume that we want to obtain the date and server headers from the response.

We will also need to specify the number of headers we want to obtain, so we will also store this number in a global variable.

#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"

const char* ssid = "yourNetworkName";
const char* password = "yourNetworkPassword";

const char * headerKeys[] = {"date", "server"};
const size_t numberOfHeaders = 2;

Next, we will move on to the Arduino setup, which we will use to open a Serial connection to output the results of our program. Additionally, we will connect the ESP8266 to the WiFi network on the setup function.

void setup () {

  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.println("Connecting..");

  }

  Serial.println("Connected to WiFi Network");
}

We will send the requests periodically on the Arduino loop function. So, we will need an object of class HTTPClient, which will make available the methods needed to send the requests and also to collect the headers of the responses.

HTTPClient http;

Now we need to initialize the request by calling the begin method on the HTTPClient object we have just created. As input, we need to pass the URL that is the destination of our HTTP request.

For illustration purposes, we will use this endpoint from the already mentioned online fake testing REST API, which returns in the response the headers we have declared previously in a global variable. It will also return other headers but, to keep the code simple, we are only going to focus on these two.

http.begin("http://jsonplaceholder.typicode.com/posts");

Now, before sending the actual request, we will need to specify which headers we want to collect from the response. To do that, we need to call the collectHeaders method on our HTTPClient object.

This method receives as first input an array of strings with the name of the headers we want to collect, and as second input the number of headers we want to collect. This is why we did the initial declaration of the headerKeys and numberOfHeaders variables.

Note that all the other headers returned in the response that were not specified in the array are not going to be available.

http.collectHeaders(headerKeys, numberOfHeaders);

Now, we send the actual request to the server by calling the GET method on our HTTPClient object. This method will return the HTTP response code returned by the server, if the request was successfully sent. If an internal error has occurred in the ESP8266, then it will return a value lesser than 0.

int httpCode = http.GET();

Now, assuming the request was successfully sent, we need to obtain the returned headers. We can check how many headers are available from the list we initially specified by calling the headers method on our HTTPClient object.

This method will only return the number of headers available but not their actual values. So, we can use the returned value in a loop to obtain the value of each available header.

To get the value of a specific header by its number, we can call the header method, passing as input the number of the header we want to obtain. As already mentioned, only headers specified in the array passed to the collectHeaders method will be available.

for(int i = 0; i< http.headers(); i++){
  Serial.println(http.header(i));
}

As alternative, we can call the overloaded version of the header method which receives as input the name of a header we want and returns its value, if that header exists in the response and was specified in our array.

String headerDate = http.header("date");
Serial.println(headerDate);

String headerServer = http.header("server");
Serial.println(headerServer);

After processing the request, we need to call the end method on our HTTPClient object to free all the resources.

http.end();

The final source code can be seen below. It has some additional checks to allow only to make the HTTP request if we are still connected to the WiFi network and to confirm that the request was successfully sent by analyzing if the returned code is lesser of greater than zero.

It also includes a small delay between each iteration of the loop to avoid sending too many requests in a short time.

#include "ESP8266WiFi.h"
#include "ESP8266HTTPClient.h"

const char* ssid = "yourNetworkName";
const char* password =  "yourNetworkPassword";

const char * headerKeys[] = {"date", "server"} ;
const size_t numberOfHeaders = 2;

void setup () {

  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);
    Serial.println("Connecting..");

  }

  Serial.println("Connected to WiFi Network");
}

void loop() {

  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    HTTPClient http;

    http.begin("http://jsonplaceholder.typicode.com/posts"); 

    http.collectHeaders(headerKeys, numberOfHeaders);

    int httpCode = http.GET();

    if (httpCode > 0) {

      for(int i = 0; i< http.headers(); i++){
        Serial.println(http.header(i));
      }

      String headerDate = http.header("date");
      Serial.println(headerDate);                     

      String headerServer = http.header("server");
      Serial.println(headerServer); 

      Serial.println("--------------------");

    }else{
      Serial.println("An error occurred sending the request");
    }

    http.end();

  }

  delay(10000);

}

 

Testing the code

To test the code, simply compile it and upload it to your ESP866 device using the Arduino IDE, with support for the Arduino core.

Once the procedure finishes, open the Arduino IDE serial monitor. Upon a successful connection to the WiFi network, it should start performing the requests periodically and printing the value of the headers returned, as shown in figure 1.

ESP8266 Arduino Get HTTP Request Response Headers.png

Figure 1 – Printing the headers returned in the HTTP response.

Remember that we are testing two different ways of printing the headers, which is why they are being printed twice per request.


Related Posts

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s