Lua
Lua
REST through HTTP Proxy
See more REST Examples
Demonstrates how to connect through an HTTP proxy 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.
-- This example connects to a REST server through an HTTP proxy.
-- It will connect to the Amazon AWS service for this example.
local rest = chilkat.Rest.new()
local socket = chilkat.Socket.new()
-- Set the HTTP proxy domain or IP address, and port.
socket.http_proxy_hostname = "192.168.1.100"
socket.http_proxy_port = 8088
-- We want to tell the socket object that we're doing HTTP over the proxy connection.
-- This is because an HTTP proxy used for other protocols (IMAP, SMTP, SSH, FTP, etc.)
-- can require some internal differences in behavior (i.e. how we do things).
socket.http_proxy_for_http = true
-- Other properties exist for specifying a proxy login, password, and authentication method (such as NTLM),
-- but these are not used in this example.
-- Connect through the HTTP proxy to the Amazon AWS server for the S3 service.
local bTls = true
local port = 443
local maxWaitMs = 5000
local success = socket:connect("s3.amazonaws.com", port, bTls, maxWaitMs)
if not success then
print("Connect Failure Error Code: " .. socket.connect_fail_reason)
print(socket.last_error_text)
return
end
-- Use the proxied TLS connection:
success = rest:use_connection(socket, 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()