Sample code for 30+ languages & platforms
JavaScript

Enumerate REST Response Header Names

See more REST Examples

Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.
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;
}

//  Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
//  and ResponseHdrValue returns the value at the same index.
var numHeaders = rest.NumResponseHeaders;
var i;
for (i = 0; i <= numHeaders - 1; i++) {
    var name = rest.ResponseHdrName(i);
    if (rest.LastMethodSuccess == false) {
        console.log(rest.LastErrorText);
        return;
    }

    var value = rest.ResponseHdrValue(i);
    if (rest.LastMethodSuccess == false) {
        console.log(rest.LastErrorText);
        return;
    }

    console.log(name + ": " + value);
}