Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PureBasic) Lightspeed - Upload an ImageCreate (uploads) a new product image. For more information, see https://developers.lightspeedhq.com/ecom/endpoints/productimage/#post-create-a-new-image
IncludeFile "CkBinData.pb" IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.pb" IncludeFile "CkHttpResponse.pb" Procedure ChilkatExample() ; This example assumes the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. http.i = CkHttp::ckCreate() If http.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success.i ; 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 CkHttp::setCkLogin(http, "API_KEY") CkHttp::setCkPassword(http, "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" ; } ; } bdImage.i = CkBinData::ckCreate() If bdImage.i = 0 Debug "Failed to create object." ProcedureReturn EndIf success = CkBinData::ckLoadFile(bdImage,"qa_data/jpg/starfish.jpg") If success = 0 Debug "Failed to load image file." CkHttp::ckDispose(http) CkBinData::ckDispose(bdImage) ProcedureReturn EndIf json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(json,"productImage.attachment",CkBinData::ckGetEncoded(bdImage,"base64")) CkJsonObject::ckUpdateString(json,"productImage.filename","starfish.jpg") CkHttp::ckSetRequestHeader(http,"Content-Type","application/json") ; The product ID in the URL is "112265200". resp.i = CkHttp::ckPostJson3(http,"https://api.webshopapp.com/en/products/112265200/images.json","application/json",json) If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) CkBinData::ckDispose(bdImage) CkJsonObject::ckDispose(json) ProcedureReturn EndIf sbResponseBody.i = CkStringBuilder::ckCreate() If sbResponseBody.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkHttpResponse::ckGetBodySb(resp,sbResponseBody) jResp.i = CkJsonObject::ckCreate() If jResp.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckLoadSb(jResp,sbResponseBody) CkJsonObject::setCkEmitCompact(jResp, 0) Debug "Response Body:" Debug CkJsonObject::ckEmit(jResp) respStatusCode.i = CkHttpResponse::ckStatusCode(resp) Debug "Response Status Code = " + Str(respStatusCode) If respStatusCode >= 400 Debug "Response Header:" Debug CkHttpResponse::ckHeader(resp) Debug "Failed." CkHttpResponse::ckDispose(resp) CkHttp::ckDispose(http) CkBinData::ckDispose(bdImage) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndIf CkHttpResponse::ckDispose(resp) ; 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 productImageId.i = CkJsonObject::ckIntOf(jResp,"productImage.id") productImageSortOrder.i = CkJsonObject::ckIntOf(jResp,"productImage.sortOrder") productImageCreatedAt.s = CkJsonObject::ckStringOf(jResp,"productImage.createdAt") productImageUpdatedAt.s = CkJsonObject::ckStringOf(jResp,"productImage.updatedAt") productImageExtension.s = CkJsonObject::ckStringOf(jResp,"productImage.extension") productImageSize.i = CkJsonObject::ckIntOf(jResp,"productImage.size") productImageTitle.s = CkJsonObject::ckStringOf(jResp,"productImage.title") productImageThumb.s = CkJsonObject::ckStringOf(jResp,"productImage.thumb") productImageSrc.s = CkJsonObject::ckStringOf(jResp,"productImage.src") CkHttp::ckDispose(http) CkBinData::ckDispose(bdImage) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.