Zig
Zig
Shopware 6 - Create Category
See more Shopware 6 Examples
Create a new category.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/category
// {
// "name": "Test123"
// }
// Create a category named "Test123"
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("name", "Test123") 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/category", 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;
}
}