Zig Requires Chilkat v11.0.0+
Zig
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 Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const tunnel = try chilkat.Socket.init();
defer tunnel.deinit();
const ssh_hostname = "sftp.example.com";
const ssh_port = 22;
// Connect to an SSH server and establish the SSH tunnel:
tunnel.sshOpenTunnel(ssh_hostname, ssh_port) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
return;
};
// Authenticate with the SSH server via a login/password
// or with a public key.
// This example demonstrates SSH password authentication.
tunnel.sshAuthenticatePw("mySshLogin", "mySshPassword") catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
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.
const rest = try chilkat.Rest.init();
defer rest.deinit();
const b_tls = true;
const port = 443;
const max_wait_ms = 5000;
// This returns a socket object that is a single channel within the SSH tunnel.
const channel = try chilkat.Socket.init();
defer channel.deinit();
tunnel.sshNewChannel("s3.amazonaws.com", port, b_tls, max_wait_ms, channel) catch {
std.debug.print("{s}\n", .{try tunnel.getLastErrorText(alloc)});
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.)
rest.useConnection(channel, true) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Provide AWS credentials for the REST call.
const auth_aws = try chilkat.AuthAws.init();
defer auth_aws.deinit();
auth_aws.setAccessKey("AWS_ACCESS_KEY");
auth_aws.setSecretKey("AWS_SECRET_KEY");
auth_aws.setServiceName("s3");
try rest.setAuthAws(auth_aws);
// List all buckets for the account...
const response_xml = rest.fullRequestNoBody(alloc, "GET", "/") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const xml = try chilkat.Xml.init();
defer xml.deinit();
try xml.loadXml(response_xml);
// Show the full XML returned.
std.debug.print("{s}\n", .{try xml.getXml(alloc)});
// Iterate over the buckets, showing each bucket name.
try xml.findChild2("Buckets");
if (xml.firstChild2()) {
std.debug.print("{s}\n", .{try xml.getChildContent(alloc, "Name")});
while (xml.nextSibling2()) {
std.debug.print("{s}\n", .{try xml.getChildContent(alloc, "Name")});
}
}
// Move the internal pointer back to the root node.
xml.getRoot2();
}