Sample code for 30+ languages & platforms
Swift

Get a REST Response Header by Name

See more REST Examples

Demonstrates Rest.ResponseHdrByName, which returns the value of a response header field by name. Header field names are case-insensitive.

Background. Response headers are available after ReadResponseHeader has read the status line and headers. Lookup by name is convenient when a specific header, such as Content-Type, is needed.

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
    }

    //  Return the value of a response header field by name.  Header field names are case-insensitive.
    var contentType: String? = rest.responseHdr(byName: "Content-Type")
    if rest.lastMethodSuccess == false {
        print("\(rest.lastErrorText!)")
        return
    }

    print("Content-Type: \(contentType!)")

}