Zig
Zig
QuickGetBd Example
The QuickGetBd method is called to send a GET request to download a binary target, such as PDF, zip, image file, etc. into 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 example assumes 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);
const bd = try chilkat.BinData.init();
defer bd.deinit();
// Download the contents of a PDF file into the bd object.
http.quickGetBd("https://www.chilkatsoft.com/hello.pdf", bd) catch {
// This will happen if the response status code was 400 or greater,
// or if the request could not be sent, or if no response was received.
//
// In other words, success will only be true if bd contains the desired content at the URL (not an error response).
const status_code = http.getLastStatus();
std.debug.print("Response status: {d}\n", .{status_code});
if (status_code == 0) {
// There was an error in communications.
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
} else {
// We received a response status code indicating failure.
// Examine the response body.
std.debug.print("{s}\n", .{try http.getLastResponseBody(alloc)});
}
return;
};
// Load the downloaded PDF into a Chilkat PDF object.
const pdf = try chilkat.Pdf.init();
defer pdf.deinit();
pdf.loadBd(bd) catch {
std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
return;
};
// ...
std.debug.print("Success.\n", .{});
}