Sample code for 30+ languages & platforms
Lua

Square API - Create Catalog Image

See more Square Examples

Uploads an image file to be represented by an CatalogImage object linked to an existing CatalogObject instance.

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 https://connect.squareup.com/v2/catalog/images \
--   -X POST \
--   -H 'Square-Version: 2020-07-22' \
--   -H 'Authorization: Bearer ACCESS_TOKEN' \
--   -H 'Accept: application/json' \
--   -F 'file=@/local/path/to/file.jpg' \
--   -F 'request={
--     "idempotency_key": "528dea59-7bfb-43c1-bd48-4a6bba7dd61f86",
--     "object_id": "ND6EA5AAJEO5WL3JNNIAQA32",
--     "image": {
--       "id": "#TEMP_ID",
--       "type": "IMAGE",
--       "image_data": {
--         "caption": "A picture of a cup of coffee"
--       }
--     }
--   }'

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

local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/v2/catalog/images"
req.content_type = "multipart/form-data"
local success = req:add_file_for_upload2("file", "/local/path/to/file.jpg", "image/jpeg")

-- Make sure to use an object_id for an already-existing catalog item.
local json = chilkat.JsonObject.new()
json:update_string("idempotency_key", "528dea59-7bfb-43c1-bd48-4a6bba7dd61f86")
json:update_string("object_id", "2PTZYUOFGGDMT75UZLHQAPAC")
json:update_string("image.id", "#TEMP_ID")
json:update_string("image.type", "IMAGE")
json:update_string("image.image_data.caption", "A picture of a cup of coffee")

req:add_param("request", json:emit())

req:add_header("Expect", "100-continue")
req:add_header("Authorization", "Bearer ACCESS_TOKEN")
req:add_header("Accept", "application/json")
req:add_header("Square-Version", "2020-07-22")

-- This example uses the sandbox: connect.squareupsandbox.com
-- Production should use connect.squareup.com
local resp = chilkat.HttpResponse.new()
success = http:http_sreq("connect.squareupsandbox.com", 443, true, req, 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)

-- {
--   "image": {
--     "type": "IMAGE",
--     "id": "UBXKMBQ4QK47QXMNQD6ELCZT",
--     "updated_at": "2020-08-07T15:20:09.779Z",
--     "version": 1596813609779,
--     "is_deleted": false,
--     "present_at_all_locations": true,
--     "image_data": {
--       "name": "",
--       "url": "https://square-catalog-sandbox.s3.amazonaws.com/files/168973eab6f8b39b5cbdad52e3f82b23be196e2b/original.jpeg",
--       "caption": "A picture of a cup of coffee"
--     }
--   }
-- }

-- 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 imageType = jResp:string_of("image.type")
local imageId = jResp:string_of("image.id")
local imageUpdated_at = jResp:string_of("image.updated_at")
local imageVersion = jResp:int_of("image.version")
local imageIs_deleted = jResp:bool_of("image.is_deleted")
local imagePresent_at_all_locations = jResp:bool_of("image.present_at_all_locations")
local imageImage_dataName = jResp:string_of("image.image_data.name")
local imageImage_dataUrl = jResp:string_of("image.image_data.url")
local imageImage_dataCaption = jResp:string_of("image.image_data.caption")