Zig
Zig
HTTP Download any Type of File (binary or text)
See more HTTP Examples
TheDownload method can download any file type, whether binary (e.g., .zip, .pdf) or text (.xml, .txt), without distinction. It streams the file byte-for-byte from the web server exactly as received. This same process applies to web pages: providing a URL typically viewed in a browser downloads the server's delivered HTML to a 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 example 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);
// Download a .zip
var local_file_path: [:0]const u8 = "/temp/hamlet.zip";
success = if (http.download("https://www.chilkatsoft.com/hamlet.zip", local_file_path)) 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("Download success, response status = {d}\n", .{status_code});
}
// Download an XML file:
local_file_path = "/temp/hamlet.xml";
success = if (http.download("https://www.chilkatsoft.com/hamlet.xml", local_file_path)) true else |_| false;
// ...
// Check for errors in the same way as shown above..
// ...
std.debug.print("OK!\n", .{});
}