Zig
Zig
Download Image (JPG, GIF, etc.) to Base64
See more HTTP Examples
Demonstrates how to download an image, or any type of file, to get the data in base64 encoding format.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);
const bd = try chilkat.BinData.init();
defer bd.deinit();
success = if (http.downloadBd("https://www.chilkatsoft.com/images/starfish.jpg", 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("Download success, response status = {d}\n", .{status_code});
const base64_image_data = try bd.getEncoded(alloc, "base64");
std.debug.print("image data in base64 format:\n", .{});
std.debug.print("{s}\n", .{base64_image_data});
}
}