Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local tunnel = chilkat.Socket.new()
local sshHostname = "sftp.example.com"
local sshPort = 22
-- Connect to an SSH server and establish the SSH tunnel:
local success = tunnel:ssh_open_tunnel(sshHostname, sshPort)
if not success then
print(tunnel.last_error_text)
return
end
-- Authenticate with the SSH server via a login/password
-- or with a public key.
-- This example demonstrates SSH password authentication.
success = tunnel:ssh_authenticate_pw("mySshLogin", "mySshPassword")
if not success then
print(tunnel.last_error_text)
return
end
-- 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.
local rest = chilkat.Rest.new()
local bTls = true
local port = 443
local maxWaitMs = 5000
-- This returns a socket object that is a single channel within the SSH tunnel.
local channel = chilkat.Socket.new()
success = tunnel:ssh_new_channel("s3.amazonaws.com", port, bTls, maxWaitMs, channel)
if not success then
print(tunnel.last_error_text)
return
end
-- 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:use_connection(channel, true)
if not success then
print(rest.last_error_text)
return
end
-- Provide AWS credentials for the REST call.
local authAws = chilkat.AuthAws.new()
authAws.access_key = "AWS_ACCESS_KEY"
authAws.secret_key = "AWS_SECRET_KEY"
authAws.service_name = "s3"
success = rest:set_auth_aws(authAws)
-- List all buckets for the account...
local responseXml = rest:full_request_no_body("GET", "/")
if not rest.last_method_success then
print(rest.last_error_text)
return
end
local xml = chilkat.Xml.new()
success = xml:load_xml(responseXml)
-- Show the full XML returned.
print(xml:get_xml())
-- Iterate over the buckets, showing each bucket name.
success = xml:find_child2("Buckets")
if xml:first_child2() then
print(xml:get_child_content("Name"))
while xml:next_sibling2() do
print(xml:get_child_content("Name"))
end
end
-- Move the internal pointer back to the root node.
xml:get_root2()