Sample code for 30+ languages & platforms
Zig

REST through SOCKS Proxy

See more REST Examples

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

Chilkat Zig Downloads

Zig
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.

    // This example connects to a REST server through a SOCKS proxy.
    // It will connect to the Amazon AWS service for this example.
    const rest = try chilkat.Rest.init();
    defer rest.deinit();
    const socket = try chilkat.Socket.init();
    defer socket.deinit();

    // Set the SOCKS proxy domain or IP address, port, and SOCKS version number (4 or 5)
    socket.setSocksHostname("192.168.1.100");
    socket.setSocksPort(1080);
    socket.setSocksVersion(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.
    const b_tls = true;
    const port = 443;
    const max_wait_ms = 5000;
    socket.connect("s3.amazonaws.com", port, b_tls, max_wait_ms) catch {
        std.debug.print("Connect Failure Error Code: {d}\n", .{socket.getConnectFailReason()});
        std.debug.print("{s}\n", .{try socket.getLastErrorText(alloc)});
        return;
    };

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