Sample code for 30+ languages & platforms
Swift

Configure HTTP Basic Authentication with SecureString

See more REST Examples

Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.

Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.

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
    }

    //  SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
    //  the credentials encrypted in memory.
    //  Normally you would not hard-code secrets in source.  You should instead obtain them from an
    //  interactive prompt, environment variable, or a secrets vault.
    let ssUsername = CkoSecureString()!
    ssUsername.append(str: "myUsername")
    let ssPassword = CkoSecureString()!
    ssPassword.append(str: "myPassword")

    success = rest.setAuthBasicSecure(username: ssUsername, password: ssPassword)
    if success == false {
        print("\(rest.lastErrorText!)")
        return
    }

    var responseText: String? = rest.fullRequestNoBody(httpVerb: "GET", uriPath: "/api/protected")
    if rest.lastMethodSuccess == false {
        print("\(rest.lastErrorText!)")
        return
    }

    print("\(responseText!)")

}