Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Tcl Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Tcl) REST through SSH Tunnel

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

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

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

set tunnel [new_CkSocket]

set sshHostname "sftp.example.com"
set sshPort 22

# Connect to an SSH server and establish the SSH tunnel:
set success [CkSocket_SshOpenTunnel $tunnel $sshHostname $sshPort]
if {$success != 1} then {
    puts [CkSocket_lastErrorText $tunnel]
    delete_CkSocket $tunnel
    exit
}

# Authenticate with the SSH server via a login/password
# or with a public key.
# This example demonstrates SSH password authentication.
set success [CkSocket_SshAuthenticatePw $tunnel "mySshLogin" "mySshPassword"]
if {$success != 1} then {
    puts [CkSocket_lastErrorText $tunnel]
    delete_CkSocket $tunnel
    exit
}

#  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.
set rest [new_CkRest]

# channel is a CkSocket

set bTls 1
set port 443
set maxWaitMs 5000

# This returns a socket object that is a single channel within the SSH tunnel.
set channel [CkSocket_SshOpenChannel $tunnel "s3.amazonaws.com" $port $bTls $maxWaitMs]
if {[CkSocket_get_LastMethodSuccess $tunnel] != 1} then {
    puts [CkSocket_lastErrorText $tunnel]
    delete_CkSocket $tunnel
    delete_CkRest $rest
    exit
}

# 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.)
set success [CkRest_UseConnection $rest $channel 1]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkSocket $channel

    delete_CkSocket $tunnel
    delete_CkRest $rest
    exit
}

# Provide AWS credentials for the REST call.
set authAws [new_CkAuthAws]

CkAuthAws_put_AccessKey $authAws "AWS_ACCESS_KEY"
CkAuthAws_put_SecretKey $authAws "AWS_SECRET_KEY"
CkAuthAws_put_ServiceName $authAws "s3"
set success [CkRest_SetAuthAws $rest $authAws]

# List all buckets for the account...
set responseXml [CkRest_fullRequestNoBody $rest "GET" "/"]
if {[CkRest_get_LastMethodSuccess $rest] != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkSocket $channel

    delete_CkSocket $tunnel
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    exit
}

set xml [new_CkXml]

set success [CkXml_LoadXml $xml $responseXml]

# Show the full XML returned.
puts [CkXml_getXml $xml]

# Iterate over the buckets, showing each bucket name.
set success [CkXml_FindChild2 $xml "Buckets"]
if {[CkXml_FirstChild2 $xml] == 1} then {
    puts [CkXml_getChildContent $xml Name]
    while {[CkXml_NextSibling2 $xml] == 1} {
        puts [CkXml_getChildContent $xml Name]
    }
}

# Move the internal pointer back to the root node.
CkXml_GetRoot2 $xml

delete_CkSocket $channel


delete_CkSocket $tunnel
delete_CkRest $rest
delete_CkAuthAws $authAws
delete_CkXml $xml

 

© 2000-2022 Chilkat Software, Inc. All Rights Reserved.