Sample code for 30+ languages & platforms
JavaScript

Get a REST Response Header Value by Index

See more REST Examples

Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.
Note
This example is intended for running within a Chilkat.Js embedded JavaScript engine. All Chilkat JavaScript examples require Chilkat v11.4.0 or greater.
JavaScript
var success = false;

var rest = new CkRest();
var bTls = true;
var bAutoReconnect = true;
success = rest.Connect("example.com",443,bTls,bAutoReconnect);
if (success == false) {
    console.log(rest.LastErrorText);
    return;
}

success = rest.SendReqNoBody("GET","/api/items/123");
if (success == false) {
    console.log(rest.LastErrorText);
    return;
}

var statusCode = rest.ReadResponseHeader();
if (statusCode < 0) {
    console.log(rest.LastErrorText);
    return;
}

//  Return the value of the response header at a zero-based index.  Use the same index with
//  ResponseHdrName to get the corresponding field name.
var numHeaders = rest.NumResponseHeaders;
var i;
for (i = 0; i <= numHeaders - 1; i++) {
    var value = rest.ResponseHdrValue(i);
    if (rest.LastMethodSuccess == false) {
        console.log(rest.LastErrorText);
        return;
    }

    console.log("Header " + i + " value: " + value);
}