Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Load XML Object from HTTP Response Body

Demonstrates how to load the HTTP response body directly into an XML object.

Chilkat Zig Downloads

Zig
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/inventory.xml", "", "", "", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const xml = try chilkat.Xml.init();
    defer xml.deinit();

    // If we wish to transfer (instead of copy) the XML from the HttpResponse to the Xml, then add the keyword "TakeResponseBody" to UncommonOptions
    // This could save memory for extremely large XML responses.
    resp.setUncommonOptions("TakeResponseBody");

    resp.getBodyXml(xml) catch {};

    std.debug.print("{s}\n", .{try xml.getXml(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)});
}