Zig
Zig
Set a Multipart Part Body from BinData
See more REST Examples
Demonstrates Rest.SetMultipartBodyBd, which sets the binary body of the selected multipart part from a BinData.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
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;
};
// The file paths are relative to the application's current working directory. Absolute paths
// may also be used. Supply the paths appropriate to your own environment.
// 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", "image/png") catch {};
// Set the binary body of the selected part from a BinData.
const bd_body = try chilkat.BinData.init();
defer bd_body.deinit();
bd_body.loadFile("qa_data/image.png") catch {
std.debug.print("Failed to load the part body file.\n", .{});
return;
};
rest.setMultipartBodyBd(bd_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});
}