Sample code for 30+ languages & platforms
Zig

Shopware Upload Media (JPG File)

See more Shopware Examples

Demonstrates how to upload a media file to a Shopware shop.

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.

    // {
    //   "file": "data:image/jpeg;base64,BASE64_DATA_HERE",
    //   "album": -9,
    //   "description": "image description",
    //   "userId": 5,
    //   "name": "my_image"
    // }

    const sb_file_data = try chilkat.StringBuilder.init();
    defer sb_file_data.deinit();
    sb_file_data.append("data:image/jpeg;base64,") catch {};
    const bd_file_data = try chilkat.BinData.init();
    defer bd_file_data.deinit();
    bd_file_data.loadFile("qa_data/jpg/starfish.jpg") catch {
        std.debug.print("Failed to load file.\n", .{});
        return;
    };

    sb_file_data.append(try bd_file_data.getEncoded(alloc, "base64")) catch {};

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("file", try sb_file_data.getAsString(alloc)) catch {};
    json.updateInt("album", -9) catch {};
    json.updateString("description", "image description") catch {};
    json.updateInt("userId", 5) catch {};
    json.updateString("name", "my_image") catch {};

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://my-shopware-shop.com/api/media", 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": 6862,
    //     "location": "http:\/\/my-shopware-shop.com\/api\/media\/6862"
    //   }
    // }

    // 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");
}