Sample code for 30+ languages & platforms
Zig

REST Receive Response in Chunks

See more REST Examples

Demonstrates how to receive a REST HTTP response in chunks.

Note: This example requires Chilkat 10.1.0 or greater.

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();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the web server
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    rest.connect("chilkatsoft.com", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Send the request.
    // This can be *any* kind of request: POST, GET, PUT, etc. using *any* of the Chilkat REST methods that send requests.
    // For this example, we'll just GET a simple XML document that is about 274K in size.

    rest.sendReqNoBody("GET", "/hamlet.xml") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Get the response header.
    const status_code = rest.readResponseHeader();
    if (status_code < 0) {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    }

    std.debug.print("response status code = {d}\n", .{status_code});

    const output_file = "c:/temp/qa_output/hamlet.xml";

    const fac = try chilkat.FileAccess.init();
    defer fac.deinit();
    try fac.openForWrite(output_file);
    if (status_code < 0) {
        std.debug.print("{s}\n", .{try fac.getLastErrorText(alloc)});
        return;
    }

    // Get the response in chunks.
    // (Note: There are more efficient ways to simply download a file from a web server, such as by calling Chilkat's Http.Download method.
    // The purpose of this method is to show how to receive a response chunk-by-chunk.)
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    var status: i32 = 1;
    while (status == 1) {
        // Read a minimum of 16000 bytes.
        // Note: Because of TLS message lengths, or the possibility of the response being either compressed (gzip/deflate) or in the HTTP chunked encoding,
        // the amount of data received in each call can be greater than the specified min size.
        // Chilkat will return from the call as soon as it has received an amount equal to or more than the specified size,
        // except for the very last chunk, which can be less that the min size or even 0 bytes.

        // The status will be one of three values:
        // -1 = error
        // 0 = received the last chunk of the response.
        // 1 = received a chunk, and more chunks are coming..

        // The received data is *appended* to the contents of the BinData object.
        status = rest.readRespChunkBd(16000, bd);
        if (status >= 0) {
            std.debug.print("Received chunk: {d} bytes\n", .{bd.getNumBytes()});
            fac.fileWriteBd(bd, 0, 0) catch {};
            bd.clear() catch {};
        }
    }

    fac.fileClose();

    std.debug.print("Success.\n", .{});
}