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

Parse Multipart Binary Http Response

See more HTTP Examples

This example demonstrates how to parse an HTTP response that is multipart and contains a binary file, such as a .zip or .pdf.

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();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();
    const req = try chilkat.HttpRequest.init();
    defer req.deinit();

    // ...
    // Insert code here to construct some kind of HTTP request.
    // this example is to show how to parse a particular kind of response.
    // ...
    // ...

    // Send the request (whatever it may be in your case) to get the HTTP response object.
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpSReq("www.somedomain.com", 443, true, req, resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // Get the response body (which is expected to be binary)
    const resp_body = try chilkat.BinData.init();
    defer resp_body.deinit();
    resp.getBodyBd(resp_body) catch {};

    // For this example, the response body contains something like this:

    // ------=_Part_21302_2029949381.1547401515443
    // Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
    // Content-Transfer-Encoding: 8bit
    // Content-ID: <rootpart@ws.jboss.org>
    //
    // <env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'><env:Header></env:Header><env:Body>...</env:Body></env:Envelope>
    // ------=_Part_21302_2029949381.1547401515443
    // Content-Type: application/octet-stream
    // Content-Transfer-Encoding: binary
    // Content-Id: <fileArchivio-7d302908-4d64-43d3-bf4e-79ce806d43b3@ws.jboss.org>
    //
    // BINARY_CONTENT_HERE...
    //
    // ------=_Part_21302_2029949381.1547401515443--
    //

    // Load it into a Chilkat MIME object.
    const mime = try chilkat.Mime.init();
    defer mime.deinit();
    mime.loadMimeBd(resp_body) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    const num_parts = mime.getNumParts();
    if (num_parts < 2) {
        std.debug.print("Expected multipart MIME with at least 2 sub-parts.\n", .{});
        return;
    }

    // Get the 1st sub-part, which is the XML.

    const part0 = try chilkat.Mime.init();
    defer part0.deinit();
    mime.partAt(0, part0) catch {};

    // Should be OK because we checked NumParts above..
    const xml_str = try part0.getBodyDecoded(alloc);
    std.debug.print("{s}\n", .{xml_str});
    std.debug.print("----\n", .{});

    // Save the 2nd part to a file.  (It is a .zip file in our test case..)

    const part1 = try chilkat.Mime.init();
    defer part1.deinit();
    mime.partAt(1, part1) catch {};

    try part1.saveBody("qa_output/attachedZip.zip");

    // Alternatively, we could extract the binary data to a BinData and use elsewhere..
    const zip_data = try chilkat.BinData.init();
    defer zip_data.deinit();
    try part1.getBodyBd(zip_data);
    try zip_data.writeFile("qa_output/attachedZip_again.zip");

    std.debug.print("OK.\n", .{});
}