Sample code for 30+ languages & platforms
Zig

REST Read Binary Response into BinData Object (Amazon S3)

Demonstrates how to make a REST call that returns a binary response body. This example is valid for files that fit in memory. The example shows how to download a JPG from the Amazon S3 service.

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.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the Amazon AWS REST server.
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("s3.amazonaws.com", port, b_tls, b_auto_reconnect);

    // ----------------------------------------------------------------------------
    // Important: For buckets created in regions outside us-east-1,
    // there are three important changes that need to be made.
    // See Working with S3 Buckets in Non-us-east-1 Regions for the details.
    // ----------------------------------------------------------------------------

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

    // Set the bucket name via the HOST header.
    // In this case, the bucket name is "chilkat100".
    rest.setHost("chilkat100.s3.amazonaws.com");

    // Send the request to download the JPG.
    rest.sendReqNoBody("GET", "/starfish.jpg") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Read the response header.
    const response_status_code = rest.readResponseHeader();
    if (response_status_code < 0) {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    }

    std.debug.print("Response status code = {d}\n", .{response_status_code});

    // We expect a 200 response status if the JPG data is coming.
    // Otherwise, we'll get a string response body with an error message(or no response body).
    if (response_status_code != 200) {
        const err_response = rest.readRespBodyString(alloc) catch "";
        if (!rest.getLastMethodSuccess()) {
            std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        } else {
            std.debug.print("{s}\n", .{err_response});
        }

        return;
    }

    const bd_body = try chilkat.BinData.init();
    defer bd_body.deinit();
    rest.readRespBd(bd_body) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Save the contents of the response body to a file.
    if (bd_body.writeFile("qa_output/starfish.jpg")) {
        std.debug.print("Success\n", .{});
    } else |_| {
        std.debug.print("Failed to write file.\n", .{});
    }
}