Sample code for 30+ languages & platforms
Zig

Xero Get File Content (Files API)

Demonstrates how to download the content of a Xero file.

Note: This example requires Chilkat v9.5.0.64 or greater.

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

    // Note: Requires Chilkat v9.5.0.64 or greater.

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

    // Before sending REST API calls, the REST object needs to be
    // initialized for OAuth1.
    // See Xero 2-Legged OAuth1 Setup for sample code.

    // Assuming the REST object's OAuth1 authenticator is setup, and the initial
    // connection was made, we may now send REST HTTP requests..

    // ------------------------------------------------------------------------------
    const file_id = "f042e9a3-a31d-4595-b8b3-6030ea6084bb";

    const sb_path = try chilkat.StringBuilder.init();
    defer sb_path.deinit();
    sb_path.append("/files.xro/1.0/Files/{FileId}/Content") catch {};
    _ = sb_path.replace("{FileId}", file_id);

    const jpg_data = try chilkat.BinData.init();
    defer jpg_data.deinit();
    rest.fullRequestNoBodyBd("GET", try sb_path.getAsString(alloc), jpg_data) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // A 200 response is expected for actual success.
    // If we don't get a 200 response, then the response body was not actually
    // the file data, but it was text containing error information.
    if (rest.getResponseStatusCode() != 200) {
        const sb_error_text = try chilkat.StringBuilder.init();
        defer sb_error_text.deinit();
        sb_error_text.appendBd(jpg_data, "utf-8", 0, 0) catch {};
        std.debug.print("{s}\n", .{try sb_error_text.getAsString(alloc)});
        std.debug.print("-- Failed.\n", .{});
        return;
    }

    // Save to a local file.
    jpg_data.writeFile("qa_output/xero_penguins.jpg") catch {
        std.debug.print("Failed to save to local file.\n", .{});
    };

    std.debug.print("Xero Get File was Successful.\n", .{});
}