Sample code for 30+ languages & platforms
Swift

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

Swift

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
    }

    //  Enumerate the response header fields by zero-based index.  ResponseHdrName returns the field name
    //  and ResponseHdrValue returns the value at the same index.
    var numHeaders: Int = rest.numResponseHeaders.intValue
    var i: Int
    for i = 0; i <= numHeaders - 1; i++ {
        var name: String? = rest.responseHdrName(index: i)
        if rest.lastMethodSuccess == false {
            print("\(rest.lastErrorText!)")
            return
        }

        var value: String? = rest.responseHdrValue(index: i)
        if rest.lastMethodSuccess == false {
            print("\(rest.lastErrorText!)")
            return
        }

        print("\(name!): \(value!)")
    }


}