Lua
Lua
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 Lua Downloads
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
-- Return the value of the response header at a zero-based index. Use the same index with
-- ResponseHdrName to get the corresponding field name.
local numHeaders = rest.num_response_headers
for i = 0, numHeaders - 1 do
local value = rest:response_hdr_value(i)
if not rest.last_method_success then
print(rest.last_error_text)
return
end
print("Header " .. i .. " value: " .. value)
end