Sample code for 30+ languages & platforms
Zig

Example: Http.DownloadAppend method

Demonstrates the DownloadAppend method.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    const target_path = "c:/temp/qa_output/download.txt";

    const http = try chilkat.Http.init();
    defer http.deinit();
    http.setKeepResponseBody(true);

    // Assume the target file in the local filesystem does not yet exist.
    try http.downloadAppend("https://chilkatsoft.com/testData/helloWorld.txt", target_path);
    var status_code: i32 = http.getLastStatus();
    if (status_code == 0) {
        // Unable to either send the request or get the response.
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    }

    // Should be 200.
    std.debug.print("Response status code: {d}\n", .{status_code});

    // Examine the contents of the file.
    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    sb.loadFile(target_path, "utf-8") catch {};
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    // Output:
    // Response status code: 200
    // Hello World!

    // Download another text file and append to the target file.
    try http.downloadAppend("https://chilkatsoft.com/testData/this_is_a_test.txt", target_path);
    status_code = http.getLastStatus();
    if (status_code == 0) {
        // Unable to either send the request or get the response.
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    }

    // Should be 200.
    std.debug.print("Response status code: {d}\n", .{status_code});

    // Examine the contents of the file.
    sb.loadFile(target_path, "utf-8") catch {};
    std.debug.print("{s}\n", .{try sb.getAsString(alloc)});

    // Output:
    // Response status code: 200
    // Hello World!This is a Test.

    // Delete the local target file.
    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    fac.fileDelete(target_path) catch {};

    // Try to download a file that does not exist:
    try http.downloadAppend("https://chilkatsoft.com/testData/does_not_exist.txt", target_path);
    status_code = http.getLastStatus();
    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, and we already know it wasn't a 200 success response.
        // It should be a 404 not found.
        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)});
    }
}