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
(Swift 3,4,5...) 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/
func chilkatTest() { // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. let http = CkoHttp()! var success: Bool // 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" // } // ] // } let json = CkoJsonObject()! json.update("default_carrier_account", value: "078870331023437cb917f5187429b093") json.update("default_servicelevel_token", value: "usps_priority") json.update("label_filetype", value: "PDF_4x6") json.update("metadata", value: "BATCH #170") json.update("batch_shipments[0].shipment.address_from.name", value: "Mr Hippo") json.update("batch_shipments[0].shipment.address_from.street1", value: "965 Mission St") json.update("batch_shipments[0].shipment.address_from.street2", value: "Ste 201") json.update("batch_shipments[0].shipment.address_from.city", value: "San Francisco") json.update("batch_shipments[0].shipment.address_from.state", value: "CA") json.update("batch_shipments[0].shipment.address_from.zip", value: "94103") json.update("batch_shipments[0].shipment.address_from.country", value: "US") json.update("batch_shipments[0].shipment.address_from.phone", value: "4151234567") json.update("batch_shipments[0].shipment.address_from.email", value: "mrhippo@goshippo.com") json.update("batch_shipments[0].shipment.address_to.name", value: "Mrs Hippo") json.update("batch_shipments[0].shipment.address_to.company", value: "") json.update("batch_shipments[0].shipment.address_to.street1", value: "Broadway 1") json.update("batch_shipments[0].shipment.address_to.street2", value: "") json.update("batch_shipments[0].shipment.address_to.city", value: "New York") json.update("batch_shipments[0].shipment.address_to.state", value: "NY") json.update("batch_shipments[0].shipment.address_to.zip", value: "10007") json.update("batch_shipments[0].shipment.address_to.country", value: "US") json.update("batch_shipments[0].shipment.address_to.phone", value: "4151234567") json.update("batch_shipments[0].shipment.address_to.email", value: "mrshippo@goshippo.com") json.update("batch_shipments[0].shipment.parcels[0].length", value: "5") json.update("batch_shipments[0].shipment.parcels[0].width", value: "5") json.update("batch_shipments[0].shipment.parcels[0].height", value: "5") json.update("batch_shipments[0].shipment.parcels[0].distance_unit", value: "in") json.update("batch_shipments[0].shipment.parcels[0].weight", value: "2") json.update("batch_shipments[0].shipment.parcels[0].mass_unit", value: "oz") json.update("batch_shipments[1].shipment.address_from.name", value: "Mr Hippo") json.update("batch_shipments[1].shipment.address_from.street1", value: "1092 Indian Summer Ct") json.update("batch_shipments[1].shipment.address_from.city", value: "San Jose") json.update("batch_shipments[1].shipment.address_from.state", value: "CA") json.update("batch_shipments[1].shipment.address_from.zip", value: "95122") json.update("batch_shipments[1].shipment.address_from.country", value: "US") json.update("batch_shipments[1].shipment.address_from.phone", value: "4151234567") json.update("batch_shipments[1].shipment.address_from.email", value: "mrhippo@goshippo.com") json.update("batch_shipments[1].shipment.address_to.name", value: "Mrs Hippo") json.update("batch_shipments[1].shipment.address_to.company", value: "") json.update("batch_shipments[1].shipment.address_to.street1", value: "Broadway 1") json.update("batch_shipments[1].shipment.address_to.street2", value: "") json.update("batch_shipments[1].shipment.address_to.city", value: "New York") json.update("batch_shipments[1].shipment.address_to.state", value: "NY") json.update("batch_shipments[1].shipment.address_to.zip", value: "10007") json.update("batch_shipments[1].shipment.address_to.country", value: "US") json.update("batch_shipments[1].shipment.address_to.phone", value: "4151234567") json.update("batch_shipments[1].shipment.address_to.email", value: "mrshippo@goshippo.com") json.update("batch_shipments[1].shipment.parcels[0].length", value: "5") json.update("batch_shipments[1].shipment.parcels[0].width", value: "5") json.update("batch_shipments[1].shipment.parcels[0].height", value: "5") json.update("batch_shipments[1].shipment.parcels[0].distance_unit", value: "in") json.update("batch_shipments[1].shipment.parcels[0].weight", value: "20") json.update("batch_shipments[1].shipment.parcels[0].mass_unit", value: "lb") json.update("batch_shipments[1].carrier_account", value: "a4391cd4ab974f478f55dc08b5c8e3b3") json.update("batch_shipments[1].servicelevel_token", value: "fedex_2_day") http.setRequestHeader("Authorization", value: "ShippoToken shippo_test_3af5d574e2f845d30efcaf9b2f47d9c2aef4807a") http.setRequestHeader("Content-Type", value: "application/json") var resp: CkoHttpResponse? = http.postJson3("https://api.goshippo.com/batches/", contentType: "application/json", json: json) if http.lastMethodSuccess == false { print("\(http.lastErrorText!)") return } let sbResponseBody = CkoStringBuilder()! resp!.getBodySb(sbResponseBody) let jResp = CkoJsonObject()! jResp.loadSb(sbResponseBody) jResp.emitCompact = false print("Response Body:") print("\(jResp.emit()!)") var respStatusCode: Int = resp!.statusCode.intValue print("Response Status Code = \(respStatusCode)") if respStatusCode >= 400 { print("Response Header:") print("\(resp!.header!)") print("Failed.") resp = nil return } resp = nil // 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 var object_id: String? = jResp.string(of: "object_id") var object_owner: String? = jResp.string(of: "object_owner") var status: String? = jResp.string(of: "status") var object_created: String? = jResp.string(of: "object_created") var object_updated: String? = jResp.string(of: "object_updated") var metadata: String? = jResp.string(of: "metadata") var default_carrier_account: String? = jResp.string(of: "default_carrier_account") var default_servicelevel_token: String? = jResp.string(of: "default_servicelevel_token") var label_filetype: String? = jResp.string(of: "label_filetype") var batch_shipmentsNext: String? = jResp.string(of: "batch_shipments.next") var batch_shipmentsPrevious: String? = jResp.string(of: "batch_shipments.previous") var object_resultsPurchase_succeeded: Int = jResp.int(of: "object_results.purchase_succeeded").intValue var object_resultsPurchase_failed: Int = jResp.int(of: "object_results.purchase_failed").intValue var object_resultsCreation_failed: Int = jResp.int(of: "object_results.creation_failed").intValue var object_resultsCreation_succeeded: Int = jResp.int(of: "object_results.creation_succeeded").intValue var i: Int = 0 var count_i: Int = jResp.size(ofArray: "batch_shipments.results").intValue while i < count_i { jResp.i = i i = i + 1 } i = 0 count_i = jResp.size(ofArray: "label_url").intValue while i < count_i { jResp.i = i i = i + 1 } } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.