Sample code for 30+ languages & platforms
Zig

Keeping Query Params in a Hashtable

Demonstrates how to use a Chilkat Hashtable to hold query params and re-emit as a query param string.

Note: The ToQueryString method was added in Chilkat v9.5.0.92.

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

    // Add name/value pairs from an existing query param string where the values are already URL encoded.
    var qp: [:0]const u8 = "txt=this%20is%20a%20test&color=blue&fruits=apples%26oranges";
    try ht.addQueryParams(qp);

    // Add some additional name/value pairs.
    ht.addStr("name", "Jack Sparrow") catch {};
    ht.addStr("movie", "Pirates of the Carribean") catch {};

    // Iterate over the hash table to show what we have..

    const keys = try chilkat.StringTable.init();
    defer keys.deinit();
    ht.getKeys(keys) catch {};
    const num_keys = keys.getCount();
    var key: [:0]const u8 = "";
    var val: [:0]const u8 = "";
    var i: i32 = 0;
    while (i < num_keys) {
        key = try keys.stringAt(alloc, i);
        val = try ht.lookupStr(alloc, key);
        std.debug.print("{s} = {s}\n", .{ key, val });
        i = i + 1;
    }

    // Output for the above loop:

    // color = blue
    // name = Jack Sparrow
    // fruits = apples&oranges
    // txt = this is a test
    // movie = Pirates of the Carribean

    // Get all name/value pairs as a query parm string with values URL encoded
    qp = try ht.toQueryString(alloc);
    std.debug.print("{s}\n", .{qp});

    // Result:
    // color=blue&name=Jack%20Sparrow&fruits=apples%26oranges&txt=this%20is%20a%20test&movie=Pirates%20of%20the%20Carribean
}