Sample code for 30+ languages & platforms
Zig

Set a Multipart Part Body from a String

See more REST Examples

Demonstrates Rest.SetMultipartBodyString, which sets the text body of the currently selected multipart part.

Background. A multipart request is assembled part by part: the part selector chooses the part being built, header fields describe it, and a SetMultipartBody method supplies its content. The request is then sent with a multipart send or full-request 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 rest = try chilkat.Rest.init();
    defer rest.deinit();
    const b_tls = true;
    const b_auto_reconnect = true;
    rest.connect("example.com", 443, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Select the multipart part to build, and set its header fields.
    rest.setPartSelector("1");
    rest.addHeader("Content-Disposition", "form-data; name=\"field1\"") catch {};
    rest.addHeader("Content-Type", "text/plain") catch {};

    // Set the text body of the selected multipart part.
    rest.setMultipartBodyString("value1") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Send the multipart request using the configured part(s).
    const response_text = rest.fullRequestMultipart(alloc, "POST", "/api/upload") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

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