Zig
Zig
Set a Multipart Part Body from a StringBuilder
See more REST Examples
Demonstrates Rest.SetMultipartBodySb, which sets the text body of the selected multipart part from a StringBuilder.
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
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", "application/json") catch {};
// Set the text body of the selected part from a StringBuilder.
const sb_body = try chilkat.StringBuilder.init();
defer sb_body.deinit();
sb_body.append("{ \"key\": \"value\" }") catch {};
rest.setMultipartBodySb(sb_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
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});
}