Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_Req
oleobject loo_Json
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_ImageType
string ls_ImageId
string ls_ImageUpdated_at
integer li_ImageVersion
integer li_ImageIs_deleted
integer li_ImagePresent_at_all_locations
string ls_ImageImage_dataName
string ls_ImageImage_dataUrl
string ls_ImageImage_dataCaption

li_Success = 0

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")

loo_Req.HttpVerb = "POST"
loo_Req.Path = "/v2/catalog/images"
loo_Req.ContentType = "multipart/form-data"
li_Success = loo_Req.AddFileForUpload2("file","/local/path/to/file.jpg","image/jpeg")

// Make sure to use an object_id for an already-existing catalog item.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("idempotency_key","528dea59-7bfb-43c1-bd48-4a6bba7dd61f86")
loo_Json.UpdateString("object_id","2PTZYUOFGGDMT75UZLHQAPAC")
loo_Json.UpdateString("image.id","#TEMP_ID")
loo_Json.UpdateString("image.type","IMAGE")
loo_Json.UpdateString("image.image_data.caption","A picture of a cup of coffee")

loo_Req.AddParam("request",loo_Json.Emit())

loo_Req.AddHeader("Expect","100-continue")
loo_Req.AddHeader("Authorization","Bearer ACCESS_TOKEN")
loo_Req.AddHeader("Accept","application/json")
loo_Req.AddHeader("Square-Version","2020-07-22")

// This example uses the sandbox: connect.squareupsandbox.com
// Production should use connect.squareup.com
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Success = loo_Http.HttpSReq("connect.squareupsandbox.com",443,1,loo_Req,loo_Resp)
if li_Success = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Req
    destroy loo_Json
    destroy loo_Resp
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)
loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Http
    destroy loo_Req
    destroy loo_Json
    destroy loo_Resp
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

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

ls_ImageType = loo_JResp.StringOf("image.type")
ls_ImageId = loo_JResp.StringOf("image.id")
ls_ImageUpdated_at = loo_JResp.StringOf("image.updated_at")
li_ImageVersion = loo_JResp.IntOf("image.version")
li_ImageIs_deleted = loo_JResp.BoolOf("image.is_deleted")
li_ImagePresent_at_all_locations = loo_JResp.BoolOf("image.present_at_all_locations")
ls_ImageImage_dataName = loo_JResp.StringOf("image.image_data.name")
ls_ImageImage_dataUrl = loo_JResp.StringOf("image.image_data.url")
ls_ImageImage_dataCaption = loo_JResp.StringOf("image.image_data.caption")


destroy loo_Http
destroy loo_Req
destroy loo_Json
destroy loo_Resp
destroy loo_SbResponseBody
destroy loo_JResp