Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.7.0+

Box: Set Up Reusable HttpCurl Requests and Outputs

See more Box Examples

Register searchFolder and searchFile once, with deferred folder_name and file_name output rules. This review example resolves a folder directly under root and a file directly inside it. The download operation requires exactly one search result at each step and validates exact names and parent IDs. Registration and ExaminePlan send no requests. Other Box examples still have their existing standalone setups.

See the Box API guide for setup and the complete workflow.

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

    var success: bool = false;

    // Requires Chilkat 11.7.0 or later and a previously unlocked Chilkat bundle.
    // See Global Unlock Sample.
    // Guide: Box API Guide.

    success = false;

    // BEGIN ONE-TIME SETUP
    // Requires Chilkat 11.7.0. Registration sends no requests.
    // Run this once and keep the SAME HttpCurl object for later downloads.
    // This focused setup searches for a root folder and a file directly inside it.
    const curl = try chilkat.HttpCurl.init();
    defer curl.deinit();
    curl.setVar("access_token", "BOX_ACCESS_TOKEN");
    const setup_command = try chilkat.StringBuilder.init();
    defer setup_command.deinit();
    var setup_replacements: i32 = 0;

    // searchFolder: Box searches names; quotes request a phrase match.
    setup_command.setString("curl -X GET \"https://api.box.com/2.0/search?query={{folder_query}}&type=folder&content_types=name&fields=id,type,name,parent&limit=2\" -H \"Authorization: Bearer ACCESS_TOKEN\"") catch {};
    setup_replacements = setup_command.replace("ACCESS_TOKEN", try curl.getVar(alloc, "access_token"));
    curl.addFunction("searchFolder", try setup_command.getAsString(alloc)) catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    // Deferred selection: folder_name need not be set during setup.
    curl.addOutput2("searchFolder", "entries", "name", "{{folder_name}}", true, "id", "folder_id") catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    curl.addOutput2("searchFolder", "entries", "name", "{{folder_name}}", true, "name", "folder_result_name") catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    curl.addOutput2("searchFolder", "entries", "name", "{{folder_name}}", true, "parent.id", "folder_parent_id") catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    curl.addOutput("searchFolder", "total_count", "folder_search_count") catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    // searchFile: Box searches names; quotes request a phrase match.
    setup_command.setString("curl -X GET \"https://api.box.com/2.0/search?query={{file_query}}&type=file&content_types=name&fields=id,type,name,parent&limit=2&ancestor_folder_ids={{folder_id}}\" -H \"Authorization: Bearer ACCESS_TOKEN\"") catch {};
    setup_replacements = setup_command.replace("ACCESS_TOKEN", try curl.getVar(alloc, "access_token"));
    curl.addFunction("searchFile", try setup_command.getAsString(alloc)) catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    // Deferred selection: file_name need not be set during setup.
    curl.addOutput2("searchFile", "entries", "name", "{{file_name}}", true, "id", "file_id") catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    curl.addOutput2("searchFile", "entries", "name", "{{file_name}}", true, "name", "file_result_name") catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    curl.addOutput2("searchFile", "entries", "name", "{{file_name}}", true, "parent.id", "file_parent_id") catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    curl.addOutput("searchFile", "total_count", "file_search_count") catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    // END ONE-TIME SETUP
    // Search is indexed: newly uploaded/renamed items may not appear immediately.
    // Search matches phrases, not exact strings, and ancestor filtering includes
    // subfolders. The operation validates counts, exact names, and immediate parents.
    // limit=2 is enough to demonstrate an ambiguous search; total_count covers all
    // results, not just this page. This introductory example requires exactly one
    // search result at each step and does not select among multiple candidates.

    // BEGIN OPERATION -- use the previously configured object.
    // Human-readable inputs. No resource IDs are supplied by the caller.
    const folder_name = "images";
    const file_name = "penguins.jpg";
    // Encode the quoted search queries separately from the literal output filters.
    // In this build, URL variable substitution does not encode '&' in a name.
    const query_text = try chilkat.StringBuilder.init();
    defer query_text.deinit();
    query_text.setString("\"") catch {};
    query_text.append(folder_name) catch {};
    query_text.append("\"") catch {};
    curl.setVar("folder_query", try query_text.getEncoded(alloc, "url", "utf-8"));
    query_text.setString("\"") catch {};
    query_text.append(file_name) catch {};
    query_text.append("\"") catch {};
    curl.setVar("file_query", try query_text.getEncoded(alloc, "url", "utf-8"));
    if (!std.mem.eql(u8, try curl.getVar(alloc, "folder_name"), folder_name)) {
        curl.clearVar("folder_id");
        curl.clearVar("folder_result_name");
        curl.clearVar("folder_parent_id");
        curl.clearVar("folder_search_count");
        curl.clearVar("file_id");
        curl.clearVar("file_result_name");
        curl.clearVar("file_parent_id");
        curl.clearVar("file_search_count");
        curl.setVar("folder_name", folder_name);
    }

    if (!std.mem.eql(u8, try curl.getVar(alloc, "file_name"), file_name)) {
        curl.clearVar("file_id");
        curl.clearVar("file_result_name");
        curl.clearVar("file_parent_id");
        curl.clearVar("file_search_count");
        curl.setVar("file_name", file_name);
    }

    // With unchanged names, cached IDs and validation fields can be reused.
    // Clear the corresponding cached fields to refresh after a remote rename/move.
    // Examine the plan without running any HTTP requests.
    const setup_plan = try chilkat.JsonObject.init();
    defer setup_plan.deinit();
    setup_plan.setEmitCompact(false);
    const authorized_command1 = try chilkat.StringBuilder.init();
    defer authorized_command1.deinit();
    authorized_command1.setString("curl -X GET \"https://api.box.com/2.0/files/{{file_id}}/content\" -H \"Authorization: Bearer {{access_token}}\" -L") catch {};
    _ = authorized_command1.replace("{{access_token}}", try curl.getVar(alloc, "access_token"));
    curl.examinePlan(try authorized_command1.getAsString(alloc), setup_plan) catch {
        std.debug.print("{s}\n", .{try curl.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("{s}\n", .{try setup_plan.emit(alloc)});
    std.debug.print("Keep this HttpCurl object for the search-based download operation.\n", .{});
}