Sample code for 30+ languages & platforms
Swift

REST through SSH Tunnel

See more REST Examples

Demonstrates how to connect through an SSH Tunnel (via port-forwarding) to make REST API calls.

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 tunnel = CkoSocket()!

    var sshHostname: String? = "sftp.example.com"
    var sshPort: Int = 22

    // Connect to an SSH server and establish the SSH tunnel:
    success = tunnel.sshOpenTunnel(sshHostname: sshHostname, sshPort: sshPort)
    if success == false {
        print("\(tunnel.lastErrorText!)")
        return
    }

    // Authenticate with the SSH server via a login/password
    // or with a public key.
    // This example demonstrates SSH password authentication.
    success = tunnel.sshAuthenticatePw(sshLogin: "mySshLogin", sshPassword: "mySshPassword")
    if success == false {
        print("\(tunnel.lastErrorText!)")
        return
    }

    //  OK, the SSH tunnel is setup.  Now open a channel within the tunnel.
    //  (Any number of channels may be created from the same SSH tunnel.
    //  Multiple channels may coexist at the same time.)

    // This example connects to a REST server through the SSH tunnel.
    // It will connect to the Amazon AWS service for this example.
    let rest = CkoRest()!

    var bTls: Bool = true
    var port: Int = 443
    var maxWaitMs: Int = 5000

    // This returns a socket object that is a single channel within the SSH tunnel.
    let channel = CkoSocket()!
    success = tunnel.sshNewChannel(hostname: "s3.amazonaws.com", port: port, ssl: bTls, maxWaitMs: maxWaitMs, channel: channel)
    if success == false {
        print("\(tunnel.lastErrorText!)")
        return
    }

    // Use the connection.  (This connection is a TLS running on an SSH channel through an SSH tunnel.
    // In other words, TLS is wrapped within the SSH tunnel.)
    success = rest.useConnection(connection: channel, autoReconnect: true)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    // Provide AWS credentials for the REST call.
    let authAws = CkoAuthAws()!
    authAws.accessKey = "AWS_ACCESS_KEY"
    authAws.secretKey = "AWS_SECRET_KEY"
    authAws.serviceName = "s3"
    success = rest.setAuthAws(authProvider: authAws)

    // List all buckets for the account...
    var responseXml: String? = rest.fullRequestNoBody(httpVerb: "GET", uriPath: "/")
    if rest.lastMethodSuccess != true {
        print("\(rest.lastErrorText!)")
        return
    }

    let xml = CkoXml()!
    success = xml.load(xmlData: responseXml)

    // Show the full XML returned.
    print("\(xml.getXml()!)")

    // Iterate over the buckets, showing each bucket name.
    success = xml.findChild2(tagPath: "Buckets")
    if xml.firstChild2() == true {
        print("\(xml.getChildContent(tagPath: "Name")!)")
        while (xml.nextSibling2() == true) {
            print("\(xml.getChildContent(tagPath: "Name")!)")
        }

    }

    // Move the internal pointer back to the root node.
    xml.getRoot2()

}