Sample code for 30+ languages & platforms
Zig

Simple GET using REST

See more REST Examples

Demonstrates how to do a simple HTTP GET request using REST.

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

    // Connect to the REST server.
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("my-store.com", port, b_tls, b_auto_reconnect);

    var response_json: [:0]const u8 = rest.fullRequestNoBody(alloc, "GET", "/wp-json/wc/v1/products?consumer_key=YOUR_CONSUMER_KEY&consumer_secret=YOUR_CONSUMER_SECRET") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

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

    // We can alternatively do this:
    rest.clearAllQueryParams() catch {};
    rest.addQueryParam("consumer_key", "YOUR_CONSUMER_KEY") catch {};
    rest.addQueryParam("consumer_secret", "YOUR_CONSUMER_SECRET") catch {};
    response_json = rest.fullRequestNoBody(alloc, "GET", "/wp-json/wc/v1/products") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

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