ArduinoHttpClient icon indicating copy to clipboard operation
ArduinoHttpClient copied to clipboard

Example to get response headers

Open glennpierce opened this issue 5 years ago • 3 comments

Sorry if I have missed this but is there a way to read response headers. I need to access these to do some scram authentication.

I see methods like readHeaderValue and readHeader() but can't work out how it it used.

Thanks

glennpierce avatar Oct 28 '20 09:10 glennpierce

@glennpierce did you ever determine how to do this? running into a similar problem

ajwentzel avatar Nov 17 '21 16:11 ajwentzel

No sorry I didn't as my work moved on to another solution. Just as well really because of the lack of reply.

glennpierce avatar Nov 18 '21 19:11 glennpierce

The main issue is that calling responseBody eats up all the header information from the response. This might be considered a bug, or an undocumented feature.

A workaround is that you need to read the response headers first as shown in the example below, and then read the body.

int statusCode = httpclient.responseStatusCode();

// read status code
Serial.print("Status code: ");
Serial.println(statusCode);

// read headers
while (httpclient.headerAvailable()) {
    Serial.print(httpclient.readHeaderName());
    Serial.print(": ");
    Serial.println(httpclient.readHeaderValue());
}

// read response
String response = httpclient.responseBody();
Serial.print("Response: ");
Serial.println(response);

gasagna avatar Aug 05 '22 12:08 gasagna