Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oTunnel
LOCAL cSshHostname
LOCAL nSshPort
LOCAL oRest
LOCAL nBTls
LOCAL nPort
LOCAL nMaxWaitMs
LOCAL oChannel
LOCAL oAuthAws
LOCAL cResponseXml
LOCAL oXml

nSuccess := 0

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

oTunnel := CreateObject("Chilkat.Socket")

cSshHostname := "sftp.example.com"
nSshPort := 22

//  Connect to an SSH server and establish the SSH tunnel:
nSuccess := oTunnel:SshOpenTunnel(cSshHostname, nSshPort)
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

//  Authenticate with the SSH server via a login/password
//  or with a public key.
//  This example demonstrates SSH password authentication.
nSuccess := oTunnel:SshAuthenticatePw("mySshLogin", "mySshPassword")
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

//   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.
oRest := CreateObject("Chilkat.Rest")

nBTls := 1
nPort := 443
nMaxWaitMs := 5000

//  This returns a socket object that is a single channel within the SSH tunnel.
oChannel := CreateObject("Chilkat.Socket")
nSuccess := oTunnel:SshNewChannel("s3.amazonaws.com", nPort, nBTls, nMaxWaitMs, oChannel)
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    oRest:destroy()
    oChannel:destroy()
    RETURN
ENDIF

//  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.)
nSuccess := oRest:UseConnection(oChannel, 1)
IF (nSuccess != 1)
    ? oRest:LastErrorText
    oTunnel:destroy()
    oRest:destroy()
    oChannel:destroy()
    RETURN
ENDIF

//  Provide AWS credentials for the REST call.
oAuthAws := CreateObject("Chilkat.AuthAws")
oAuthAws:AccessKey := "AWS_ACCESS_KEY"
oAuthAws:SecretKey := "AWS_SECRET_KEY"
oAuthAws:ServiceName := "s3"
nSuccess := oRest:SetAuthAws(oAuthAws)

//  List all buckets for the account...
cResponseXml := oRest:FullRequestNoBody("GET", "/")
IF (oRest:LastMethodSuccess != 1)
    ? oRest:LastErrorText
    oTunnel:destroy()
    oRest:destroy()
    oChannel:destroy()
    oAuthAws:destroy()
    RETURN
ENDIF

oXml := CreateObject("Chilkat.Xml")
nSuccess := oXml:LoadXml(cResponseXml)

//  Show the full XML returned.
? oXml:GetXml()

//  Iterate over the buckets, showing each bucket name.
nSuccess := oXml:FindChild2("Buckets")
IF (oXml:FirstChild2() == 1)
    ? oXml:GetChildContent("Name")
    DO WHILE (oXml:NextSibling2() == 1)
        ? oXml:GetChildContent("Name")
    ENDDO
ENDIF

//  Move the internal pointer back to the root node.
oXml:GetRoot2()

oTunnel:destroy()
oRest:destroy()
oChannel:destroy()
oAuthAws:destroy()
oXml:destroy()