Sample code for 30+ languages & platforms
Zig

Send a REST Request with a Binary Body

See more REST Examples

Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.

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. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).

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.

    // FullRequestBd sends a request whose binary body is read from a BinData and stores the text
    // response body in a StringBuilder.
    const bd_request = try chilkat.BinData.init();
    defer bd_request.deinit();
    bd_request.loadFile("qa_data/image.png") catch {
        std.debug.print("Failed to load the request body file.\n", .{});
        return;
    };

    rest.addHeader("Content-Type", "image/png") catch {};

    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    rest.fullRequestBd("PUT", "/api/images/1", bd_request, sb_response) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const response_text = sb_response.getAsString(alloc) catch {
        std.debug.print("{s}\n", .{try sb_response.getLastErrorText(alloc)});
        return;
    };

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