Zig
Zig
Shopware 6 - Rename Category
See more Shopware 6 Examples
Changes the name of an existing 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();
// You'll need to know the category's id to update the name.
// See Find Shopware Category by Name for example code.
// 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"));
// Send a PATCH request where the category id is in the path and the new name is in the JSON body, like this:
// PATCH /api/v3/category/ab6e524bf224404cb4b675a76550b8cd
// {
// "name": "new_category_name"
// }
const category_id = "ab6e524bf224404cb4b675a76550b8cd";
const sb_url = try chilkat.StringBuilder.init();
defer sb_url.deinit();
sb_url.append("https://my-shopware-6-shop.de/api/v3/category/") catch {};
sb_url.append(category_id) catch {};
// Rename the category to "TestABC"
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("name", "TestABC") catch {};
const url = try sb_url.getAsString(alloc);
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("PATCH", url, 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 204 response status code indicates success.
// The response body will be empty when successful.
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;
}
}