Sample code for 30+ languages & platforms
Ruby

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.

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

#  Return the value of the response header at a zero-based index.  Use the same index with
#  ResponseHdrName to get the corresponding field name.
numHeaders = rest.get_NumResponseHeaders()

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

    print "Header " + i.to_s() + " value: " + value + "\n";
end