Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loTunnel
LOCAL lcSshHostname
LOCAL lnSshPort
LOCAL loRest
LOCAL lnBTls
LOCAL lnPort
LOCAL lnMaxWaitMs
LOCAL loChannel
LOCAL loAuthAws
LOCAL lcResponseXml
LOCAL loXml

lnSuccess = 0

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

loTunnel = CreateObject('Chilkat.Socket')

lcSshHostname = "sftp.example.com"
lnSshPort = 22

* Connect to an SSH server and establish the SSH tunnel:
lnSuccess = loTunnel.SshOpenTunnel(lcSshHostname,lnSshPort)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
ENDIF

* Authenticate with the SSH server via a login/password
* or with a public key.
* This example demonstrates SSH password authentication.
lnSuccess = loTunnel.SshAuthenticatePw("mySshLogin","mySshPassword")
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    CANCEL
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.
loRest = CreateObject('Chilkat.Rest')

lnBTls = 1
lnPort = 443
lnMaxWaitMs = 5000

* This returns a socket object that is a single channel within the SSH tunnel.
loChannel = CreateObject('Chilkat.Socket')
lnSuccess = loTunnel.SshNewChannel("s3.amazonaws.com",lnPort,lnBTls,lnMaxWaitMs,loChannel)
IF (lnSuccess = 0) THEN
    ? loTunnel.LastErrorText
    RELEASE loTunnel
    RELEASE loRest
    RELEASE loChannel
    CANCEL
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.)
lnSuccess = loRest.UseConnection(loChannel,1)
IF (lnSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loTunnel
    RELEASE loRest
    RELEASE loChannel
    CANCEL
ENDIF

* Provide AWS credentials for the REST call.
loAuthAws = CreateObject('Chilkat.AuthAws')
loAuthAws.AccessKey = "AWS_ACCESS_KEY"
loAuthAws.SecretKey = "AWS_SECRET_KEY"
loAuthAws.ServiceName = "s3"
lnSuccess = loRest.SetAuthAws(loAuthAws)

* List all buckets for the account...
lcResponseXml = loRest.FullRequestNoBody("GET","/")
IF (loRest.LastMethodSuccess <> 1) THEN
    ? loRest.LastErrorText
    RELEASE loTunnel
    RELEASE loRest
    RELEASE loChannel
    RELEASE loAuthAws
    CANCEL
ENDIF

loXml = CreateObject('Chilkat.Xml')
lnSuccess = loXml.LoadXml(lcResponseXml)

* Show the full XML returned.
? loXml.GetXml()

* Iterate over the buckets, showing each bucket name.
lnSuccess = loXml.FindChild2("Buckets")
IF (loXml.FirstChild2() = 1) THEN
    ? loXml.GetChildContent("Name")
    DO WHILE (loXml.NextSibling2() = 1)
        ? loXml.GetChildContent("Name")
    ENDDO
ENDIF

* Move the internal pointer back to the root node.
loXml.GetRoot2()

RELEASE loTunnel
RELEASE loRest
RELEASE loChannel
RELEASE loAuthAws
RELEASE loXml