Sample code for 30+ languages & platforms
Swift

HTTP GET with Custom Header and OAuth2 Bearer Token

See more HTTP Examples

Demonstrate how to send a GET request with customer headers and an "Authorization: Bearer " header.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    // Setting the AuthToken property causes the "Authorization: Bearer <token>" header to be adeded.
    http.authToken = "Just_the_access_token_here"

    // Add one or more custom headers..
    http.setRequestHeader(name: "X-Tenant-ID", value: "value goes here")
    http.setRequestHeader(name: "blah-blah-blah", value: "value goes here")

    var url: String? = "https://www.example.com/abc/123?x=something&y=someOtherThing"

    // Send the GET request and get the response body in the StringBuilder object.
    let sb = CkoStringBuilder()!
    success = http.quickGetSb(url: url, sbContent: sb)
    if success != true {
        print("\(http.lastErrorText!)")
        return
    }

    print("response status code: \(http.lastStatus.intValue)")
    print("response body:")
    print("\(sb.getAsString()!)")

    // If the response contains JSON, you can load it into a Chilkat JSON object...
    let json = CkoJsonObject()!
    json.loadSb(sb: sb)
    json.emitCompact = false
    print("\(json.emit()!)")

}