Zig
Zig
REST Download Binary File to Memory
See more REST Examples
Download a binary file to a Chilkat BinData object.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// 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();
// We're going to download a sample MS-Word doc file.
// The URLs of our MS-Word sample documents are:
// https://www.chilkatdownload.com/sample_data/sample.doc
// https://www.chilkatdownload.com/sample_data/sample.docx
const path_part_of_url = "/sample_data/sample.doc";
const domain = "chilkatdownload.com";
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
rest.connect(domain, port, b_tls, b_auto_reconnect) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const bd = try chilkat.BinData.init();
defer bd.deinit();
rest.fullRequestNoBodyBd("GET", path_part_of_url, bd) 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(bd, "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.
// Change the file path based on your operating system or needs...
bd.writeFile("c:/temp/qa_output/sample.doc") catch {
std.debug.print("Failed to save to local file.\n", .{});
return;
};
std.debug.print("REST Download of MS-Word File was successful.\n", .{});
}