Lua
Lua
Upsert Catalog Object
See more Square Examples
Creates or updates the target CatalogObject.Chilkat Lua Downloads
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/object \
-- -X POST \
-- -H 'Square-Version: 2020-07-22' \
-- -H 'Authorization: Bearer ACCESS_TOKEN' \
-- -H 'Content-Type: application/json' \
-- -d '{
-- "idempotency_key": "af3d1afc-7212-4300-b463-0bfc5314a5ae",
-- "object": {
-- "type": "ITEM",
-- "id": "#Cocoa",
-- "item_data": {
-- "name": "Cocoa",
-- "description": "Hot chocolate",
-- "abbreviation": "Ch"
-- }
-- }
-- }'
-- Use the following online tool to generate HTTP code from a CURL command
-- Convert a cURL Command to HTTP Source Code
-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "idempotency_key": "af3d1afc-7212-4300-b463-0bfc5314a5ae",
-- "object": {
-- "type": "ITEM",
-- "id": "#Cocoa",
-- "item_data": {
-- "name": "Cocoa",
-- "description": "Hot chocolate",
-- "abbreviation": "Ch"
-- }
-- }
-- }
local json = chilkat.JsonObject.new()
json:update_string("idempotency_key", "af3d1afc-7212-4300-b463-0bfc5314a5ae")
json:update_string("object.type", "ITEM")
json:update_string("object.id", "#Cocoa")
json:update_string("object.item_data.name", "Cocoa")
json:update_string("object.item_data.description", "Hot chocolate")
json:update_string("object.item_data.abbreviation", "Ch")
-- Adds the "Authorization: Bearer ACCESS_TOKEN" header.
http.auth_token = "ACCESS_TOKEN"
http:set_request_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()
local success = http:http_json("POST", "https://connect.squareupsandbox.com/v2/catalog/object", 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)
-- {
-- "catalog_object": {
-- "type": "ITEM",
-- "id": "7SB3ZQYJ5GDMVFL7JK46JCHT",
-- "updated_at": "2016-11-16T22:32:42.996Z",
-- "version": 1479335562996,
-- "is_deleted": false,
-- "item_data": {
-- "name": "Cocoa",
-- "description": "Hot chocolate",
-- "abbreviation": "Ch"
-- }
-- },
-- "id_mappings": [
-- {
-- "client_object_id": "#Cocoa",
-- "object_id": "7SB3ZQYJ5GDMVFL7JK46JCHT"
-- }
-- ]
-- }
-- 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 client_object_id = ""
local object_id = ""
local catalog_objectType = jResp:string_of("catalog_object.type")
local catalog_objectId = jResp:string_of("catalog_object.id")
local catalog_objectUpdated_at = jResp:string_of("catalog_object.updated_at")
local catalog_objectVersion = jResp:int_of("catalog_object.version")
local catalog_objectIs_deleted = jResp:bool_of("catalog_object.is_deleted")
local catalog_objectItem_dataName = jResp:string_of("catalog_object.item_data.name")
local catalog_objectItem_dataDescription = jResp:string_of("catalog_object.item_data.description")
local catalog_objectItem_dataAbbreviation = jResp:string_of("catalog_object.item_data.abbreviation")
local i = 0
local count_i = jResp:size_of_array("id_mappings")
while i < count_i do
jResp.i = i
client_object_id = jResp:string_of("id_mappings[i].client_object_id")
object_id = jResp:string_of("id_mappings[i].object_id")
i = i + 1
end