Sample code for 30+ languages & platforms
Zig

Lightspeed - Upload an Image

See more Lightspeed Examples

Create (uploads) a new product image.

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();

    // Implements the following CURL command:

    // curl   -u API_KEY:API_SECRET \
    //     -H "Content-Type: application/json" \
    //     -X POST \
    //     -d '{
    //   "productImage": {
    //     "attachment": "base64-encoded-contents",
    //     "filename": "the-filename-with-extension-goes-here.jpg"
    //   }
    // }' \
    // "https://api.webshopapp.com/en/products/product_id/images.json"

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    http.setLogin("API_KEY");
    http.setPassword("API_SECRET");

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    // The following JSON is sent in the request body.

    // {
    //   "productImage": {
    //     "attachment": "base64-encoded-contents",
    //     "filename": "the-filename-with-extension-goes-here.jpg"
    //   }
    // }

    const bd_image = try chilkat.BinData.init();
    defer bd_image.deinit();
    bd_image.loadFile("qa_data/jpg/starfish.jpg") catch {
        std.debug.print("Failed to load image file.\n", .{});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("productImage.attachment", try bd_image.getEncoded(alloc, "base64")) catch {};
    json.updateString("productImage.filename", "starfish.jpg") catch {};

    http.setRequestHeader("Content-Type", "application/json");

    // The product ID in the URL is "112265200".
    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://api.webshopapp.com/en/products/112265200/images.json", 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)});

    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)

    // {
    //   "productImage": {
    //     "id": 13362569,
    //     "sortOrder": 3,
    //     "createdAt": "2019-06-06T14:52:07+00:00",
    //     "updatedAt": "2019-06-06T14:52:07+00:00",
    //     "extension": "png",
    //     "size": 8016,
    //     "title": "logo",
    //     "thumb": "https://cdn.shoplightspeed.com/shops/000001/files/14271562/50x50x2/logo.png",
    //     "src": "https://cdn.shoplightspeed.com/shops/000001/files/14271562/logo.png"
    //   }
    // }

    // Sample code for parsing the JSON response...
    // Use the following online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    _ = j_resp.intOf("productImage.id");
    _ = j_resp.intOf("productImage.sortOrder");
    _ = try j_resp.stringOf(alloc, "productImage.createdAt");
    _ = try j_resp.stringOf(alloc, "productImage.updatedAt");
    _ = try j_resp.stringOf(alloc, "productImage.extension");
    _ = j_resp.intOf("productImage.size");
    _ = try j_resp.stringOf(alloc, "productImage.title");
    _ = try j_resp.stringOf(alloc, "productImage.thumb");
    _ = try j_resp.stringOf(alloc, "productImage.src");
}