Sample code for 30+ languages & platforms
Zig

WooCommerce List Products having SKU

See more WooCommerce Examples

Gets information for a product by SKU.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Implements the following CURL command:

    // curl https://example.com/wp-json/wc/v3/products?sku=3386460107914 \
    //     -u consumer_key:consumer_secret

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    http.setBasicAuth(true);
    http.setLogin("consumer_key");
    http.setPassword("consumer_secret");

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.setUrlVar("sku", "3386460107914") catch {};
    // Use "https" or "http" depending on what your site needs.
    http.quickGetSb("http://example.com/wp-json/wc/v3/products?sku={$sku}", sb_response_body) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const jarr_resp = try chilkat.JsonArray.init();
    defer jarr_resp.deinit();
    jarr_resp.loadSb(sb_response_body) catch {};
    jarr_resp.setEmitCompact(false);

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try jarr_resp.emit(alloc)});

    const resp_status_code = http.getLastStatus();
    std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
    if (resp_status_code >= 400) {
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try http.getLastHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }
}