Sample code for 30+ languages & platforms
Zig

URL-Encode a MIME Body

See more MIME Examples

Demonstrates the Chilkat Mime.UrlEncodeBody method, which replaces the current body with an application/x-www-form-urlencoded-style representation using the named charset. The only argument is the charset; it is a void method.

Background: This transforms the body into the form-encoding used by HTML form submissions — spaces to +, reserved characters to percent-escapes — interpreting the text through the given charset first so non-ASCII characters encode correctly. It is a niche operation for constructing form-style payloads within a MIME part.

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

    // Demonstrates the Mime.UrlEncodeBody method, which replaces the current body with an
    // application/x-www-form-urlencoded representation using the named charset.  The only argument is
    // the charset.  It is a void method.

    const mime = try chilkat.Mime.init();
    defer mime.deinit();

    mime.setBodyFromPlainText("name=John Doe & city=New York") catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

    // URL-encode the body: spaces become "+", reserved characters become %-escapes.
    mime.urlEncodeBody("utf-8");

    const encoded_body = mime.getBodyDecoded(alloc) catch {
        std.debug.print("{s}\n", .{try mime.getLastErrorText(alloc)});
        return;
    };

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