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) Shippo Create a BatchDemonstrates how to create and purchase up to 10,000 shipments in a single API request. For more information, see https://goshippo.com/docs/batch/
IncludeFile "CkHttpResponse.pb" IncludeFile "CkHttp.pb" IncludeFile "CkStringBuilder.pb" IncludeFile "CkJsonObject.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 ; Use this online tool to generate code from sample JSON: ; Generate Code to Create JSON ; The following JSON is sent in the request body. ; { ; "default_carrier_account": "078870331023437cb917f5187429b093", ; "default_servicelevel_token": "usps_priority", ; "label_filetype": "PDF_4x6", ; "metadata": "BATCH #170", ; "batch_shipments": [ ; { ; "shipment": { ; "address_from": { ; "name": "Mr Hippo", ; "street1": "965 Mission St", ; "street2": "Ste 201", ; "city": "San Francisco", ; "state": "CA", ; "zip": "94103", ; "country": "US", ; "phone": "4151234567", ; "email": "mrhippo@goshippo.com" ; }, ; "address_to": { ; "name": "Mrs Hippo", ; "company": "", ; "street1": "Broadway 1", ; "street2": "", ; "city": "New York", ; "state": "NY", ; "zip": "10007", ; "country": "US", ; "phone": "4151234567", ; "email": "mrshippo@goshippo.com" ; }, ; "parcels": [ ; { ; "length": "5", ; "width": "5", ; "height": "5", ; "distance_unit": "in", ; "weight": "2", ; "mass_unit": "oz" ; } ; ] ; } ; }, ; { ; "shipment": { ; "address_from": { ; "name": "Mr Hippo", ; "street1": "1092 Indian Summer Ct", ; "city": "San Jose", ; "state": "CA", ; "zip": "95122", ; "country": "US", ; "phone": "4151234567", ; "email": "mrhippo@goshippo.com" ; }, ; "address_to": { ; "name": "Mrs Hippo", ; "company": "", ; "street1": "Broadway 1", ; "street2": "", ; "city": "New York", ; "state": "NY", ; "zip": "10007", ; "country": "US", ; "phone": "4151234567", ; "email": "mrshippo@goshippo.com" ; }, ; "parcels": [ ; { ; "length": "5", ; "width": "5", ; "height": "5", ; "distance_unit": "in", ; "weight": "20", ; "mass_unit": "lb" ; } ; ] ; }, ; "carrier_account": "a4391cd4ab974f478f55dc08b5c8e3b3", ; "servicelevel_token": "fedex_2_day" ; } ; ] ; } json.i = CkJsonObject::ckCreate() If json.i = 0 Debug "Failed to create object." ProcedureReturn EndIf CkJsonObject::ckUpdateString(json,"default_carrier_account","078870331023437cb917f5187429b093") CkJsonObject::ckUpdateString(json,"default_servicelevel_token","usps_priority") CkJsonObject::ckUpdateString(json,"label_filetype","PDF_4x6") CkJsonObject::ckUpdateString(json,"metadata","BATCH #170") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.name","Mr Hippo") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.street1","965 Mission St") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.street2","Ste 201") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.city","San Francisco") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.state","CA") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.zip","94103") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.country","US") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.phone","4151234567") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_from.email","mrhippo@goshippo.com") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.name","Mrs Hippo") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.company","") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.street1","Broadway 1") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.street2","") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.city","New York") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.state","NY") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.zip","10007") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.country","US") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.phone","4151234567") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.address_to.email","mrshippo@goshippo.com") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.parcels[0].length","5") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.parcels[0].width","5") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.parcels[0].height","5") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.parcels[0].distance_unit","in") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.parcels[0].weight","2") CkJsonObject::ckUpdateString(json,"batch_shipments[0].shipment.parcels[0].mass_unit","oz") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_from.name","Mr Hippo") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_from.street1","1092 Indian Summer Ct") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_from.city","San Jose") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_from.state","CA") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_from.zip","95122") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_from.country","US") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_from.phone","4151234567") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_from.email","mrhippo@goshippo.com") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.name","Mrs Hippo") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.company","") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.street1","Broadway 1") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.street2","") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.city","New York") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.state","NY") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.zip","10007") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.country","US") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.phone","4151234567") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.address_to.email","mrshippo@goshippo.com") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.parcels[0].length","5") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.parcels[0].width","5") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.parcels[0].height","5") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.parcels[0].distance_unit","in") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.parcels[0].weight","20") CkJsonObject::ckUpdateString(json,"batch_shipments[1].shipment.parcels[0].mass_unit","lb") CkJsonObject::ckUpdateString(json,"batch_shipments[1].carrier_account","a4391cd4ab974f478f55dc08b5c8e3b3") CkJsonObject::ckUpdateString(json,"batch_shipments[1].servicelevel_token","fedex_2_day") CkHttp::ckSetRequestHeader(http,"Authorization","ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a") CkHttp::ckSetRequestHeader(http,"Content-Type","application/json") resp.i = CkHttp::ckPostJson3(http,"https://api.goshippo.com/batches/","application/json",json) If CkHttp::ckLastMethodSuccess(http) = 0 Debug CkHttp::ckLastErrorText(http) CkHttp::ckDispose(http) 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) 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) ; { ; "object_id": "a015eb693cca465dbb6523ce6d2e3c65", ; "object_owner": "admin", ; "status": "VALIDATING", ; "object_created": "2016-09-12T15:25:43.465Z", ; "object_updated": "2016-09-12T15:25:43.465Z", ; "metadata": "BATCH #170", ; "default_carrier_account": "078870331023437cb917f5187429b093", ; "default_servicelevel_token": "usps_priority", ; "label_filetype": "PDF_4x6", ; "batch_shipments": { ; "next": null, ; "previous": null, ; "results": [ ; ] ; }, ; "object_results": { ; "purchase_succeeded": 0, ; "purchase_failed": 0, ; "creation_failed": 0, ; "creation_succeeded": 0 ; }, ; "label_url": [ ; ] ; } ; Sample code for parsing the JSON response... ; Use the following online tool to generate parsing code from sample JSON: ; Generate Parsing Code from JSON object_id.s = CkJsonObject::ckStringOf(jResp,"object_id") object_owner.s = CkJsonObject::ckStringOf(jResp,"object_owner") status.s = CkJsonObject::ckStringOf(jResp,"status") object_created.s = CkJsonObject::ckStringOf(jResp,"object_created") object_updated.s = CkJsonObject::ckStringOf(jResp,"object_updated") metadata.s = CkJsonObject::ckStringOf(jResp,"metadata") default_carrier_account.s = CkJsonObject::ckStringOf(jResp,"default_carrier_account") default_servicelevel_token.s = CkJsonObject::ckStringOf(jResp,"default_servicelevel_token") label_filetype.s = CkJsonObject::ckStringOf(jResp,"label_filetype") batch_shipmentsNext.s = CkJsonObject::ckStringOf(jResp,"batch_shipments.next") batch_shipmentsPrevious.s = CkJsonObject::ckStringOf(jResp,"batch_shipments.previous") object_resultsPurchase_succeeded.i = CkJsonObject::ckIntOf(jResp,"object_results.purchase_succeeded") object_resultsPurchase_failed.i = CkJsonObject::ckIntOf(jResp,"object_results.purchase_failed") object_resultsCreation_failed.i = CkJsonObject::ckIntOf(jResp,"object_results.creation_failed") object_resultsCreation_succeeded.i = CkJsonObject::ckIntOf(jResp,"object_results.creation_succeeded") i.i = 0 count_i.i = CkJsonObject::ckSizeOfArray(jResp,"batch_shipments.results") While i < count_i CkJsonObject::setCkI(jResp, i) i = i + 1 Wend i = 0 count_i = CkJsonObject::ckSizeOfArray(jResp,"label_url") While i < count_i CkJsonObject::setCkI(jResp, i) i = i + 1 Wend CkHttp::ckDispose(http) CkJsonObject::ckDispose(json) CkStringBuilder::ckDispose(sbResponseBody) CkJsonObject::ckDispose(jResp) ProcedureReturn EndProcedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.