Zig Requires Chilkat v11.0.0+
Zig
Load JSON Object from HTTP Response Body
Demonstrates how to load the HTTP response body directly into a JSON object.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 http = try chilkat.Http.init();
defer http.deinit();
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpStr("GET", "https://www.chilkatsoft.com/exampledata/sample.json", "", "", "", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
// A JSON object is JSON that begins with "{" and ends with "}"
const json = try chilkat.JsonObject.init();
defer json.deinit();
// If we wish to transfer (instead of copy) the JSON from the HttpResponse to the JsonObject, then add the keyword "TakeResponseBody" to UncommonOptions
// This could save memory for extremely large JSON responses.
resp.setUncommonOptions("TakeResponseBody");
resp.getBodyJson(json) catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Note: If UncommonOptions contained "TakeResponseBody", then the response BodyStr will now be empty:
std.debug.print("----\n", .{});
std.debug.print("{s}\n", .{try resp.getBodyStr(alloc)});
}