Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

local rest = chilkat.Rest.new()
local bTls = true
local bAutoReconnect = true
local success = rest:connect("example.com", 443, bTls, bAutoReconnect)
if not success then
    print(rest.last_error_text)
    return
end

success = rest:send_req_no_body("GET", "/api/items/123")
if not success then
    print(rest.last_error_text)
    return
end

local statusCode = rest:read_response_header()
if statusCode < 0 then
    print(rest.last_error_text)
    return
end

-- Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
-- and ResponseHdrValue returns the value at the same index.
local numHeaders = rest.num_response_headers
for i = 0, numHeaders - 1 do
    local name = rest:response_hdr_name(i)
    if not rest.last_method_success then
        print(rest.last_error_text)
        return
    end

    local value = rest:response_hdr_value(i)
    if not rest.last_method_success then
        print(rest.last_error_text)
        return
    end

    print(name .. ": " .. value)
end