Rust Requires Chilkat v11.0.0+
Rust
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 Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let tunnel = chilkat::Socket::new();
let ssh_hostname = "sftp.example.com".to_string();
let ssh_port = 22;
// Connect to an SSH server and establish the SSH tunnel:
if tunnel.ssh_open_tunnel(&ssh_hostname, ssh_port).is_err() {
println!("{}", tunnel.last_error_text());
return;
}
// Authenticate with the SSH server via a login/password
// or with a public key.
// This example demonstrates SSH password authentication.
if tunnel.ssh_authenticate_pw("mySshLogin", "mySshPassword").is_err() {
println!("{}", tunnel.last_error_text());
return;
}
// 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.
let rest = chilkat::Rest::new();
let b_tls = true;
let port = 443;
let max_wait_ms = 5000;
// This returns a socket object that is a single channel within the SSH tunnel.
let channel = chilkat::Socket::new();
if tunnel.ssh_new_channel("s3.amazonaws.com", port, b_tls, max_wait_ms, &channel).is_err() {
println!("{}", tunnel.last_error_text());
return;
}
// 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.)
if rest.use_connection(&channel, true).is_err() {
println!("{}", rest.last_error_text());
return;
}
// Provide AWS credentials for the REST call.
let auth_aws = chilkat::AuthAws::new();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("AWS_SECRET_KEY");
auth_aws.set_service_name("s3");
let _ = rest.set_auth_aws(&auth_aws).is_ok();
// List all buckets for the account...
let Ok(response_xml) = rest.full_request_no_body("GET", "/") else {
println!("{}", rest.last_error_text());
return;
};
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&response_xml).is_ok();
// Show the full XML returned.
println!("{}", xml.get_xml().unwrap_or_default());
// Iterate over the buckets, showing each bucket name.
let _ = xml.find_child2("Buckets").is_ok();
if xml.first_child2().is_ok() {
println!("{}", xml.get_child_content("Name").unwrap_or_default());
while xml.next_sibling2().is_ok() {
println!("{}", xml.get_child_content("Name").unwrap_or_default());
}
}
// Move the internal pointer back to the root node.
xml.get_root2();