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

Extract Files from Binary SOAP MTOM MIME

See more MIME Examples

This example demonstrates how to extract files from a binary SOAP MTOM MIME document.

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 mime = try chilkat.Mime.init();
    defer mime.deinit();

    // In this example, we have a MIME file containing 8bit (non-encoded) binary data,
    // and it is what I call "headless".  MIME is headless when it omits
    // the top-level header.  The file we have here begins with the first
    // boundary string.

    // The structure the MIME to be loaded is:

    // multipart/mixed (inferred because it is headless)
    //     application/xop+xml
    //     image/jpeg
    //     image/gif
    //     image/gif
    //

    mime.loadMimeFile("qa_data/mime/headless_binary_soap_mtom_mime.mim") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    // The MIME file loaded in this example contains this:

    // --uuid:e74486f4-52b0-44b6-b829-156810fae20d
    // Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml"
    // Content-Transfer-Encoding: binary
    // Content-ID: <root.message@cxf.apache.org>
    //
    // <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body> ... </soap:Body></soap:Envelope>
    // --uuid:e74486f4-52b0-44b6-b829-156810fae20d
    // Content-Type: image/jpeg
    // Content-Transfer-Encoding: binary
    // Content-ID: <beee83b7-166c-494c-890a-def990e9887b-1496@cxf.apache.org>
    // Content-Disposition: attachment;name="-2049913191"
    //
    // BINARY DATA HERE...
    //
    // --uuid:e74486f4-52b0-44b6-b829-156810fae20d
    // Content-Type: image/gif
    // Content-Transfer-Encoding: binary
    // Content-ID: <beee83b7-166c-494c-890a-def990e9887b-1497@cxf.apache.org>
    // Content-Disposition: attachment;name="-2049913188"
    //
    // BINARY DATA HERE...
    //
    // --uuid:e74486f4-52b0-44b6-b829-156810fae20d
    // Content-Type: image/gif
    // Content-Transfer-Encoding: binary
    // Content-ID: <beee83b7-166c-494c-890a-def990e9887b-1498@cxf.apache.org>
    // Content-Disposition: attachment;name="-2049913185"
    //
    // BINARY DATA HERE...
    //
    // --uuid:e74486f4-52b0-44b6-b829-156810fae20d--

    // Get the number of MIME sub-parts.
    const num_parts = mime.getNumParts();

    // The 1st part at index 0 is the application/xop+xml.  We're just going to extract the JPG and GIF image files..
    const sb_filename = try chilkat.StringBuilder.init();
    defer sb_filename.deinit();
    var name: [:0]const u8 = "";
    var i: i32 = 1;
    const mp = try chilkat.Mime.init();
    defer mp.deinit();
    while (i < num_parts) {
        mime.partAt(i, mp) catch {};

        // By looking at the MIME above, the "name" attribute of the Content-Disposition header field seems
        // to be the only possible name we can use for each image..
        sb_filename.append("qa_output/") catch {};
        name = try mp.getHeaderFieldAttribute(alloc, "Content-Disposition", "name");
        sb_filename.append(name) catch {};
        sb_filename.append(".") catch {};
        sb_filename.append(try mp.getContentType(alloc)) catch {};
        _ = sb_filename.replace("image/", "");
        mp.saveBody(try sb_filename.getAsString(alloc)) catch {};
        std.debug.print("output file: {s}\n", .{try sb_filename.getAsString(alloc)});
        sb_filename.clear();
        i = i + 1;
    }

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