Sample code for 30+ languages & platforms
Lua

Lightspeed - Upload an Image

See more Lightspeed Examples

Create (uploads) a new product image.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

local http = chilkat.Http.new()

-- 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.login = "API_KEY"
http.password = "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"
--   }
-- }

local bdImage = chilkat.BinData.new()
local success = bdImage:load_file("qa_data/jpg/starfish.jpg")
if not success then
    print("Failed to load image file.")
    return
end

local json = chilkat.JsonObject.new()
json:update_string("productImage.attachment", bdImage:get_encoded("base64"))
json:update_string("productImage.filename", "starfish.jpg")

http:set_request_header("Content-Type", "application/json")

-- The product ID in the URL is "112265200".
local resp = chilkat.HttpResponse.new()
success = http:http_json("POST", "https://api.webshopapp.com/en/products/112265200/images.json", json, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false

print("Response Body:")
print(jResp:emit())

local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
    print("Response Header:")
    print(resp.header)
    print("Failed.")
    return
end

-- 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

local productImageId = jResp:int_of("productImage.id")
local productImageSortOrder = jResp:int_of("productImage.sortOrder")
local productImageCreatedAt = jResp:string_of("productImage.createdAt")
local productImageUpdatedAt = jResp:string_of("productImage.updatedAt")
local productImageExtension = jResp:string_of("productImage.extension")
local productImageSize = jResp:int_of("productImage.size")
local productImageTitle = jResp:string_of("productImage.title")
local productImageThumb = jResp:string_of("productImage.thumb")
local productImageSrc = jResp:string_of("productImage.src")