arduino-libraries / ArduinoHttpClient

Arduino HTTP Client library
282 stars 170 forks source link

Example to get response headers #93

Open glennpierce opened 3 years ago

glennpierce commented 3 years ago

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

ajwentzel commented 2 years ago

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

glennpierce commented 2 years ago

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

gasagna commented 2 years ago

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);