Zig
Zig
Example: Http.DownloadSb method
See more HTTP Examples
Demonstrates theDownloadSb method.
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;
const http = try chilkat.Http.init();
defer http.deinit();
http.setKeepResponseBody(true);
const sb = try chilkat.StringBuilder.init();
defer sb.deinit();
success = if (http.downloadSb("https://chilkatsoft.com/testData/helloWorld.txt", "utf-8", sb)) 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});
std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
}
}