Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loTunnel = createobject("CkSocket")
lcSshHostname = "sftp.example.com"
lnSshPort = 22
// Connect to an SSH server and establish the SSH tunnel:
llSuccess = loTunnel.SshOpenTunnel(lcSshHostname,lnSshPort)
if (llSuccess = .F.) then
? loTunnel.LastErrorText
release loTunnel
return
endif
// Authenticate with the SSH server via a login/password
// or with a public key.
// This example demonstrates SSH password authentication.
llSuccess = loTunnel.SshAuthenticatePw("mySshLogin","mySshPassword")
if (llSuccess = .F.) then
? loTunnel.LastErrorText
release loTunnel
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.
loRest = createobject("CkRest")
llBTls = .T.
lnPort = 443
lnMaxWaitMs = 5000
// This returns a socket object that is a single channel within the SSH tunnel.
loChannel = createobject("CkSocket")
llSuccess = loTunnel.SshNewChannel("s3.amazonaws.com",lnPort,llBTls,lnMaxWaitMs,loChannel)
if (llSuccess = .F.) then
? loTunnel.LastErrorText
release loTunnel
release loRest
release loChannel
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.)
llSuccess = loRest.UseConnection(loChannel,.T.)
if (llSuccess <> .T.) then
? loRest.LastErrorText
release loTunnel
release loRest
release loChannel
return
endif
// Provide AWS credentials for the REST call.
loAuthAws = createobject("CkAuthAws")
loAuthAws.AccessKey = "AWS_ACCESS_KEY"
loAuthAws.SecretKey = "AWS_SECRET_KEY"
loAuthAws.ServiceName = "s3"
llSuccess = loRest.SetAuthAws(loAuthAws)
// List all buckets for the account...
lcResponseXml = loRest.FullRequestNoBody("GET","/")
if (loRest.LastMethodSuccess <> .T.) then
? loRest.LastErrorText
release loTunnel
release loRest
release loChannel
release loAuthAws
return
endif
loXml = createobject("CkXml")
llSuccess = loXml.LoadXml(lcResponseXml)
// Show the full XML returned.
? loXml.GetXml()
// Iterate over the buckets, showing each bucket name.
llSuccess = loXml.FindChild2("Buckets")
if (loXml.FirstChild2() = .T.) then
? loXml.GetChildContent("Name")
do while (loXml.NextSibling2() = .T.)
? 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