How to read http response headers?

Hi everyone,

my Photon connects to a server in my home network and authenticates with username and password. That works really reliable so far. To be able to send commands to the server I need to be able to read the response headers because the server sends some kind of cookie token. The httpclient library does not support Serial.println(response.headers). Is there a simple solution for this?

#include "HttpClient.h"

/**
* Declaring the variables.
*/
unsigned int nextTime = 0;    // Next time to contact the server
HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
      { "Content-Type", "application/json" },
      { "Accept" , "application/json" },
      { "x-elocs-username" , "some username" },
      { "x-elocs-password" , "some password hash" },
      { NULL, NULL } // NOTE: Always terminate headers with NULL
};

http_request_t request;
http_response_t response;

void setup() {
    Serial.begin(9600);
}

void loop() {
    if (nextTime > millis()) {
        return;
    }

    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.hostname = "some ip";
    request.port = 80;
    request.path = "/login";
    
    // The library also supports sending a body with your request:
    //request.body = "{\"key\":\"value\"}";

    // Post request
    http.post(request, response, headers);
    Serial.print("Application>\tResponse status: ");
    Serial.println(response.status);

    Serial.print("Application>\tHTTP Response Body: ");
    Serial.println(response.body);
    
    nextTime = millis() + 10000;
}

In my Browser it looks like this:

Code	Details
200	
Response body
HOME

Response headers
 connection: keep-alive  content-length: 8  content-type: text/html; charset=utf-8  date: Sun, 31 Mar 2019 09:04:23 GMT  etag: W/"8-cb36de7c"  x-elocs-language: de  x-elocs-token: here is the access token  x-elocs-version: 1.8.22189.xxx  x-powered-by: Express 

In the serial console i receive:

Start of Loop.
Application>    Response status: 200
Application>    HTTP Response Body: HOME

@ParticleD, or @mstanley are you able to assist?

I’m at the same place - looking for a way to access the response headers. Umm, nvm. :slight_smile:


    String raw_response(http.buffer);
    
    Serial.print("Application>\tHTTP Response Body: ");
    Serial.println(raw_response);

image

I found a solution with the tcpclient library a few days ago. I am currently on holidays so I will post my solution next week :slight_smile: