Zig
Zig
Send a REST Request using StringBuilder Bodies
See more REST Examples
Demonstrates Rest.FullRequestSb, which sends a complete request whose text body is read from a StringBuilder and stores the text response body in a second StringBuilder.
Background. Using StringBuilder objects for the request and response bodies avoids extra string conversions, which is helpful when working with large payloads.
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;
};
// FullRequestSb sends a request whose text body is read from a StringBuilder and stores the text
// response body in a second StringBuilder. This avoids extra string conversions for large bodies.
const sb_request = try chilkat.StringBuilder.init();
defer sb_request.deinit();
sb_request.append("{ \"query\": \"chilkat\" }") catch {};
rest.addHeader("Content-Type", "application/json") catch {};
const sb_response = try chilkat.StringBuilder.init();
defer sb_response.deinit();
rest.fullRequestSb("POST", "/api/search", sb_request, sb_response) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const response_text = sb_response.getAsString(alloc) catch {
std.debug.print("{s}\n", .{try sb_response.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{response_text});
}