Zig Requires Chilkat v11.1.0+
Zig
POST JSON to REST API with non-us-ascii Chars Escaped
See more REST Examples
Demonstrates how to POST to a REST API with non-usascii chars within JSON Unicode escaped.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();
// Connect using TLS.
const b_auto_reconnect = true;
try rest.connect("chilkatsoft.com", 443, true, b_auto_reconnect);
// Load JSON containing the following Korean text.
// {
// "BillAddr": {
// "Id": "239615",
// "Line1": "류리하",
// "Line2": "류리하류리하",
// "City": "류리하류리하",
// "Country": "US",
// "CountrySubDivisionCode": "AK",
// "PostalCode": "류리하"
// }
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.setEmitCompact(false);
json.loadFile("qa_data/json/korean.json") catch {
std.debug.print("{s}\n", .{try json.getLastErrorText(alloc)});
return;
};
try rest.addHeader("Content-Type", "application/json; charset=UTF-8");
const sb = try chilkat.StringBuilder.init();
defer sb.deinit();
json.emitSb(sb) catch {};
sb.encode("unicodeescape", "utf-8") catch {};
std.debug.print("{s}\n", .{try sb.getAsString(alloc)});
// The StringBuilder contains this:
// {
// "BillAddr": {
// "Id": "239615",
// "Line1": "\ub958\ub9ac\ud558",
// "Line2": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
// "City": "\ub958\ub9ac\ud558\ub958\ub9ac\ud558",
// "Country": "US",
// "CountrySubDivisionCode": "AK",
// "PostalCode": "\ub958\ub9ac\ud558"
// }
// }
const sb_resp = try chilkat.StringBuilder.init();
defer sb_resp.deinit();
rest.fullRequestSb("POST", "/echo_request_body.asp", sb, sb_resp) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// Show the response.
std.debug.print("Json Response: {s}\n", .{try sb_resp.getAsString(alloc)});
}