Zig
Zig
Shopware 6 - Create Product
See more Shopware 6 Examples
Create a new product.Chilkat Zig Downloads
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();
// Sends the following POST
// POST /api/v3/product/
// {
// "name" : "Test123",
// "productNumber" : "random",
// "stock" : 10,
// "price" : [
// {
// "currencyId" : "b7d2554b0ce847cd82f3ac9bd1c0dfca",
// "gross": 15,
// "net": 10,
// "linked" : false
// }
// ],
// "tax" : {
// "name": "test",
// "taxRate": 15
// }
// }
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// Create a product named "Test123"
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("name", "Test123") catch {};
json.updateString("productNumber", "XYZ-1234") catch {};
json.updateInt("stock", 10) catch {};
json.updateString("price[0].currencyId", "b7d2554b0ce847cd82f3ac9bd1c0dfca") catch {};
json.updateInt("price[0].gross", 15) catch {};
json.updateInt("price[0].net", 10) catch {};
json.updateBool("price[0].linked", false) catch {};
json.updateString("tax.name", "test") catch {};
json.updateInt("tax.taxRate", 15) catch {};
// Load the access token previously obtained in Shopware 6 OAuth2 Client Credentials
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/shopware6.json") catch {};
// This causes the "Authorization: Bearer <access_token>" header to be added.
http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("POST", "https://my-shopware-6-shop.de/api/v3/product", 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);
// A successful response is a 204 response status code with an empty response body.
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
// If we get a 401 response, it may be that our access token expired and we need to fetch a new one.
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;
}
}