Tcl
Tcl
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 Tcl Downloads
load ./chilkat.dll
set success 0
set rest [new_CkRest]
set bTls 1
set bAutoReconnect 1
set success [CkRest_Connect $rest "example.com" 443 $bTls $bAutoReconnect]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
set success [CkRest_SendReqNoBody $rest "GET" "/api/items/123"]
if {$success == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
set statusCode [CkRest_ReadResponseHeader $rest]
if {$statusCode < 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
# Enumerate the response header fields by zero-based index. ResponseHdrName returns the field name
# and ResponseHdrValue returns the value at the same index.
set numHeaders [CkRest_get_NumResponseHeaders $rest]
for {set i 0} {$i <= [expr $numHeaders - 1]} {incr i} {
set name [CkRest_responseHdrName $rest $i]
if {[CkRest_get_LastMethodSuccess $rest] == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
set value [CkRest_responseHdrValue $rest $i]
if {[CkRest_get_LastMethodSuccess $rest] == 0} then {
puts [CkRest_lastErrorText $rest]
delete_CkRest $rest
exit
}
puts "$name: $value"
}
delete_CkRest $rest