Sample code for 30+ languages & platforms
Zig

Send a Form-URL-Encoded REST Request

See more REST Examples

Demonstrates Rest.FullRequestFormUrlEncoded, which sends an application/x-www-form-urlencoded request. The body is built from the query parameters added to the object, and the response body is returned as text.

Background. For a form-url-encoded request the accumulated parameters become the request body rather than the URL query string. This matches the encoding used by HTML form POSTs.

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 rest = try chilkat.Rest.init();
    defer rest.deinit();
    const b_tls = true;
    const b_auto_reconnect = true;
    rest.connect("example.com", 443, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // The application/x-www-form-urlencoded body is built from the query parameters added to the object.
    rest.addQueryParam("username", "alice") catch {};
    rest.addQueryParam("action", "login") catch {};

    // Send the form-url-encoded request.  The parameters become the request body rather than the URL
    // query string.
    const response_text = rest.fullRequestFormUrlEncoded(alloc, "POST", "/api/login") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

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