Sample code for 30+ languages & platforms
Zig

REST Stream Multipart Body from File

See more REST Examples

Demonstrates how to send a multipart/form-data HTTP request, where one of the parts contains data streamed directly from a file. This is good for cases where the file to be uploaded is very large.

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 destination web server.
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("www.somewebserver.com", port, b_tls, b_auto_reconnect);

    // This example will send the following multipart/form-data request.
    // The Content-Length is automatically computed and added by Chilkat.

    //     POST /some_path HTTP/1.1
    //     Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150
    //     Content-Length: 834
    //
    //     -----------------------------735323031399963166993862150
    //     Content-Disposition: form-data; name="text1"
    //
    //     text 123 abc
    //     -----------------------------735323031399963166993862150
    //     Content-Disposition: form-data; name="text2"
    //
    //     xyz
    //     -----------------------------735323031399963166993862150
    //     Content-Disposition: form-data; name="file1"; filename="a.txt"
    //     Content-Type: text/plain
    //
    //     Content of a.txt.
    //
    //     -----------------------------735323031399963166993862150
    //     Content-Disposition: form-data; name="file2"; filename="a.html"
    //     Content-Type: text/html
    //
    //     <!DOCTYPE html><title>Content of a.html.</title>
    //
    //     -----------------------------735323031399963166993862150
    //     Content-Disposition: form-data; name="file3"; filename="starfish.jpg"
    //     Content-Type: image/jpeg
    //
    //     binary data goes here
    //     -----------------------------735323031399963166993862150--

    // Set the Content-Type for the topmost MIME part.
    rest.addHeader("Content-Type", "multipart/form-data") catch {};

    // Specify each part of the request.
    rest.setPartSelector("1");
    rest.addHeader("Content-Disposition", "form-data; name=\"text1\"") catch {};
    rest.setMultipartBodyString("text 123 abc") catch {};

    rest.setPartSelector("2");
    rest.addHeader("Content-Disposition", "form-data; name=\"text2\"") catch {};
    rest.setMultipartBodyString("xyz") catch {};

    rest.setPartSelector("3");
    rest.addHeader("Content-Disposition", "form-data; name=\"file1\"; filename=\"a.txt\"") catch {};
    rest.addHeader("Content-Type", "text/plain") catch {};
    rest.setMultipartBodyString("Content of a.txt.") catch {};

    rest.setPartSelector("4");
    rest.addHeader("Content-Disposition", "form-data; name=\"file2\"; filename=\"a.html\"") catch {};
    rest.addHeader("Content-Type", "text/html") catch {};
    const sb_html = try chilkat.StringBuilder.init();
    defer sb_html.deinit();
    sb_html.loadFile("qa_data/html/a.html", "utf-8") catch {};
    rest.setMultipartBodySb(sb_html) catch {};

    rest.setPartSelector("5");
    rest.addHeader("Content-Disposition", "form-data; name=\"file3\"; filename=\"starfish.jpg\"") catch {};
    rest.addHeader("Content-Type", "image/jpeg") catch {};

    // When the request is sent, stream this part directly from the file.
    // This avoids having to load the entire file into memory.
    const file_stream = try chilkat.Stream.init();
    defer file_stream.deinit();
    file_stream.setSourceFile("qa_data/jpg/starfish.jpg");
    rest.setMultipartBodyStream(file_stream) catch {};

    _ = rest.fullRequestMultipart(alloc, "POST", "/some_path") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // ...
    // ...

    // Clear the REST object for any subsequent requests..
    rest.clearAllHeaders() catch {};
    rest.clearAllParts() catch {};
    rest.setPartSelector("");
}