Sample code for 30+ languages & platforms
Zig

POST application/x-www-form-urlencoded using REST API

See more REST Examples

Demonstrates how to send a POST with query params (x-www-form-urlencoded) using the Chilkat REST object.

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

    // This example will send to https://www.chilkatsoft.com/echoPost.asp

    // Make the initial connection (without sending a request yet).
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    rest.connect("www.chilkatsoft.com", port, b_tls, b_auto_reconnect) catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // Provide query params.
    rest.addQueryParam("firstName", "John") catch {};
    rest.addQueryParam("lastName", "Doe") catch {};
    rest.addQueryParam("company", "Bisco Bits Ltd.") catch {};

    const response_str = rest.fullRequestFormUrlEncoded(alloc, "POST", "/echoPost.asp") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    // When successful, the response status code will equal 200.
    if (rest.getResponseStatusCode() != 200) {
        // Examine the request/response to see what happened.
        std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
        std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
        std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("response body (if any): {s}\n", .{response_str});
        std.debug.print("---\n", .{});
        std.debug.print("LastRequestStartLine: {s}\n", .{try rest.getLastRequestStartLine(alloc)});
        std.debug.print("LastRequestHeader: {s}\n", .{try rest.getLastRequestHeader(alloc)});
        return;
    }

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