Sample code for 30+ languages & platforms
Zig

Configure AWS Signature Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthAws, which associates an AuthAws provider so Chilkat signs each request using AWS Signature Version 4. The provider supplies the access key, secret key, region, and service name.

Background. AWS requires each request to be signed with a signature derived from the secret key, region, service, and request contents. Configuring the provider lets Chilkat compute and attach the signature automatically.

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();

    const rest = try chilkat.Rest.init();
    defer rest.deinit();
    const b_tls = true;
    const b_auto_reconnect = true;
    rest.connect("example.com", 443, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Configure AWS Signature Version 4 request signing.  Provide the access key, secret key, region,
    // and service name.
    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    // Normally you would not hard-code secrets in source.  You should instead obtain them from an
    // interactive prompt, environment variable, or a secrets vault.
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    auth_aws.setRegion("us-east-1");
    auth_aws.setServiceName("s3");

    // Associate the provider so Chilkat signs each request according to AWS requirements.
    rest.setAuthAws(auth_aws) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const response_text = rest.fullRequestNoBody(alloc, "GET", "/") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{response_text});
}