Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
let rest = CkoRest()!
var bTls: Bool = true
var bAutoReconnect: Bool = true
success = rest.connect(hostname: "example.com", port: 443, tls: bTls, autoReconnect: bAutoReconnect)
if success == false {
print("\(rest.lastErrorText!)")
return
}
success = rest.sendReqNoBody(httpVerb: "GET", uriPath: "/api/items/123")
if success == false {
print("\(rest.lastErrorText!)")
return
}
var statusCode: Int = rest.readResponseHeader().intValue
if statusCode < 0 {
print("\(rest.lastErrorText!)")
return
}
// Return the value of the response header at a zero-based index. Use the same index with
// ResponseHdrName to get the corresponding field name.
var numHeaders: Int = rest.numResponseHeaders.intValue
var i: Int
for i = 0; i <= numHeaders - 1; i++ {
var value: String? = rest.responseHdrValue(index: i)
if rest.lastMethodSuccess == false {
print("\(rest.lastErrorText!)")
return
}
print("Header \(i) value: \(value!)")
}
}