Sample code for 30+ languages & platforms
Zig

Shopware Create a New Product

See more Shopware Examples

Create a new product in Shopware.

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

    var success: bool = false;

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

    http.setLogin("api_username");
    http.setPassword("api_key");
    http.setBasicAuth(true);

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "name": "Sport Shoes",
    //   "active": true,
    //   "tax": 19,
    //   "supplier": "Sport Shoes Inc.",
    //   "categories": [
    //     {
    //       "id": 384
    //     }
    //   ],
    //   "mainDetail": {
    //     "number": "turn",
    //     "active": true,
    //     "prices": [
    //       {
    //         "customerGroupKey": "EK",
    //         "price": 999
    //       }
    //     ]
    //   }
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("name", "Sport Shoes") catch {};
    json.updateBool("active", true) catch {};
    json.updateInt("tax", 19) catch {};
    json.updateString("supplier", "Sport Shoes Inc.") catch {};
    json.updateInt("categories[0].id", 384) catch {};
    json.updateString("mainDetail.number", "turn") catch {};
    json.updateBool("mainDetail.active", true) catch {};
    json.updateString("mainDetail.prices[0].customerGroupKey", "EK") catch {};
    json.updateInt("mainDetail.prices[0].price", 999) catch {};

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://my-shopware-shop.com/api/articles", json, "application/json", resp) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    resp.getBodySb(sb_response_body) catch {};

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

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

    // A 201 response code indicates success.
    const resp_status_code = resp.getStatusCode();
    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 resp.getHeader(alloc)});
        std.debug.print("Failed.\n", .{});
        return;
    }

    // Sample JSON response:
    // (Sample code for parsing the JSON response is shown below)

    // {
    //   "success": true,
    //   "data": {
    //     "id": 8312,
    //     "location": "https:\/\/my-shopware-shop.com\/api\/articles\/8312"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    success = j_resp.boolOf("success");
    _ = j_resp.intOf("data.id");
    _ = try j_resp.stringOf(alloc, "data.location");
}