Sample code for 30+ languages & platforms
Zig

HTTP GET with utf-8 URL Encoded Query Params

See more HTTP Examples

Demonstrates how to build URLs where query param values can be either URL encoded from the utf-8 representation, or from another charset such as windows-1252.

Note: This example uses the new DecodeAndAppend method added in Chilkat v9.5.0.87.

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

    // We have the string "MÆRSK".

    // This is the URL encoding of the windows-1252 representation.
    const name_windows1252_url_encoded = "M%C6RSK";

    const sb1 = try chilkat.StringBuilder.init();
    defer sb1.deinit();
    try sb1.decodeAndAppend(name_windows1252_url_encoded, "url", "windows-1252");

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

    // Here's how to send an HTTP GET where the param is the utf-8 representation that is URL encoded.
    // For example:  https://www.chilkatsoft.com/something?name=M%C3%A6RSK

    const sb_url = try chilkat.StringBuilder.init();
    defer sb_url.deinit();

    sb_url.append("https://www.chilkatsoft.com/something?name=") catch {};
    sb_url.append(try sb1.getEncoded(alloc, "url", "utf-8")) catch {};
    std.debug.print("{s}\n", .{try sb_url.getAsString(alloc)});

    var response_body: [:0]const u8 = try http.quickGetStr(alloc, try sb_url.getAsString(alloc));

    // Here's how to send an HTTP GET where the param is the windows-1252 representation that is URL encoded.
    // For example:  https://www.chilkatsoft.com/something?name=M%E6RSK

    sb_url.clear();
    sb_url.append("https://www.chilkatsoft.com/something?name=") catch {};
    sb_url.append(try sb1.getEncoded(alloc, "url", "windows-1252")) catch {};
    std.debug.print("{s}\n", .{try sb_url.getAsString(alloc)});

    response_body = try http.quickGetStr(alloc, try sb_url.getAsString(alloc));
}