Sample code for 30+ languages & platforms
Rust

REST through SOCKS Proxy

See more REST Examples

Demonstrates how to connect through a SOCKS proxy to make REST API calls.

Chilkat Rust Downloads

Rust

// 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 a SOCKS proxy.
// It will connect to the Amazon AWS service for this example.
let rest = chilkat::Rest::new();
let socket = chilkat::Socket::new();

// Set the SOCKS proxy domain or IP address, port, and SOCKS version number (4 or 5)
socket.set_socks_hostname("192.168.1.100");
socket.set_socks_port(1080);
socket.set_socks_version(5);
// Other properties exist for specifying a SOCKS proxy login and password,
// but these are not used in this example.

// Connect through the HTTP proxy to the Amazon AWS server for the S3 service.
let b_tls = true;
let port = 443;
let max_wait_ms = 5000;
if socket.connect("s3.amazonaws.com", port, b_tls, max_wait_ms).is_err() {
    println!("Connect Failure Error Code: {}", socket.connect_fail_reason());
    println!("{}", socket.last_error_text());
    return;
}

// Use the proxied TLS connection:
if rest.use_connection(&socket, 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();