Sample code for 30+ languages & platforms
Zig

Download a Binary REST Response into BinData

See more REST Examples

Demonstrates Rest.FullRequestNoBodyBd, which sends a request with no body and stores the binary response body in a BinData.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. This method is appropriate when the response is binary, such as an image or file download, allowing the received bytes to be saved or processed directly.

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;
    };

    // The file paths are relative to the application's current working directory.  Absolute paths
    // may also be used.  Supply the paths appropriate to your own environment.

    // FullRequestNoBodyBd sends a request with no body and stores the binary response body in a
    // BinData.  This is appropriate when the response is binary, such as an image or file download.
    const bd_response = try chilkat.BinData.init();
    defer bd_response.deinit();
    rest.fullRequestNoBodyBd("GET", "/api/images/1", bd_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    bd_response.writeFile("qa_output/image.png") catch {
        std.debug.print("Failed to save the response body.\n", .{});
        return;
    };

    std.debug.print("Downloaded {d} bytes.\n", .{bd_response.getNumBytes()});
}