Zig
Zig
Box.com Download Binary File to Memory
See more Box Examples
Retrieves the actual data of the file into a memory (not to a local file).Chilkat Zig Downloads
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;
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = try chilkat.Http.init();
defer http.deinit();
http.setKeepResponseBody(true);
// Provide a previously obtained OAuth2 access token.
// See Get Box OAuth2 Access Token.
http.setAuthToken("BOX_ACCESS_TOKEN");
http.setFollowRedirects(true);
// The file is specified by the file ID.
// In this case, we're downloading a file with FILE_ID = 283371752129.
// The general form of the URL for downloading is: https://api.box.com/2.0/files/FILE_ID/content
const bd = try chilkat.BinData.init();
defer bd.deinit();
success = if (http.downloadBd("https://api.box.com/2.0/files/283371752129/content", bd)) true else |_| false;
const status_code = http.getLastStatus();
if (!success) {
if (status_code == 0) {
// Unable to either send the request or get the response.
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
} else {
// We got a response, but the status code was not in the 200s
std.debug.print("Response status code: {d}\n", .{status_code});
// Examine the response body.
std.debug.print("Response body:\n", .{});
std.debug.print("{s}\n", .{try http.getLastResponseBody(alloc)});
}
std.debug.print("Download failed.\n", .{});
} else {
std.debug.print("Downloaded tigers.jpeg into an object holding the binary data.\n", .{});
std.debug.print("Number of bytes: {d}\n", .{bd.getNumBytes()});
}
}