Zig
Zig
Shopware Update Product Data
See more Shopware Examples
Update information about an existing product in Shopware.Chilkat Zig Downloads
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);
// The following JSON is sent in the request body.
// {
// "name": "Super-Duper Sports Shoes"
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("name", "Super-Duper Sports Shoes") catch {};
// The id of the product is appended to the path part of the URL.
http.setUrlVar("id", "8312") catch {};
const url = "https://my-shopware-shop.com/api/articles/{$id}";
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpJson("PUT", 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);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
// A 200 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");
}