Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.5.0+

curl with Path Variables and Query Param Variables

See more CURL Examples

This example demonstrates using variables located in the path and query params with the {{variable_name}} syntax.

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

    // Variables can occur in the path and query params.
    // Variable names are enclosed between {{ and }}

    //  curl -X GET https://httpbin.org/{{verb}}?id={id_value}}
    const target_curl = "curl -X GET https://httpbin.org/{{verb}}?id={{id_value}}";

    const http_curl = try chilkat.HttpCurl.init();
    defer http_curl.deinit();

    // Provide values for variables.
    // In this example, "verb" is a path variable, and "id_value" is a query param variable.
    http_curl.setVar("verb", "get");
    http_curl.setVar("id_value", "123");

    // Run the curl command.
    http_curl.doYourThing(target_curl) catch {
        std.debug.print("{s}\n", .{try http_curl.getLastErrorText(alloc)});
        return;
    };

    const response_json = try chilkat.JsonObject.init();
    defer response_json.deinit();
    response_json.setEmitCompact(false);

    const status_code = http_curl.getStatusCode();
    std.debug.print("response status code: {d}\n", .{status_code});

    http_curl.getResponseJson(response_json) catch {};
    std.debug.print("{s}\n", .{try response_json.emit(alloc)});

    // Output:

    // response status code: 200
    // {
    //   "args": {
    //     "id": "123"
    //   },
    //   "headers": {
    //     "Host": "httpbin.org",
    //     "X-Amzn-Trace-Id": "Root=1-69e92914-5d4136d240f2f7fe1056f126"
    //   },
    //   "origin": "222.222.222.222",
    //   "url": "https://httpbin.org/get?id=123"
    // }
}