Sample code for 30+ languages & platforms
Ruby

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.

Chilkat Ruby Downloads

Ruby
require 'chilkat'

success = false

rest = Chilkat::CkRest.new()
bTls = true
bAutoReconnect = true
success = rest.Connect("example.com",443,bTls,bAutoReconnect)
if (success == false)
    print rest.lastErrorText() + "\n";
    exit
end

success = rest.SendReqNoBody("GET","/api/items/123")
if (success == false)
    print rest.lastErrorText() + "\n";
    exit
end

statusCode = rest.ReadResponseHeader()
if (statusCode < 0)
    print rest.lastErrorText() + "\n";
    exit
end

#  Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
#  and ResponseHdrValue returns the value at the same index.
numHeaders = rest.get_NumResponseHeaders()

for i in 0 .. numHeaders - 1
    name = rest.responseHdrName(i)
    if (rest.get_LastMethodSuccess() == false)
        print rest.lastErrorText() + "\n";
        exit
    end

    value = rest.responseHdrValue(i)
    if (rest.get_LastMethodSuccess() == false)
        print rest.lastErrorText() + "\n";
        exit
    end

    print name + ": " + value + "\n";
end