Sample code for 30+ languages & platforms
Zig

Shopware Delete Media

See more Shopware Examples

Demonstrates how to delete a media file from 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();

    // 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 id of the product is appended to the path part of the URL.
    http.setUrlVar("id", "6864") catch {};

    const url = "https://my-shopware-shop.com/api/media/{$id}";

    // Send a DELETE request with nothing in the request body.
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpNoBody("DELETE", url, 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 (i.e. the request was sent and a response was received).
    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:

    // {
    //   "success": true
    // }

    const b_deleted = j_resp.boolOf("success");
    std.debug.print("Deleted: {}\n", .{b_deleted});

    // A failed response would look like this:

    // {
    //   "success": false,
    //   "message": "Product by \"id\" 6864 not found"
    // }
}