Swift
Swift
Shippo Adding Metadata
See more Shippo Examples
Demonstrates how to add metadata to the tracking request through a POST request.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// Implements the following CURL command:
// curl https://api.goshippo.com/tracks/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -d carrier="shippo" \
// -d tracking_number="SHIPPO_TRANSIT" \
// -d metadata="Order 000123"
let req = CkoHttpRequest()!
req.httpVerb = "POST"
req.path = "/tracks/"
req.contentType = "application/x-www-form-urlencoded"
req.addParam(name: "carrier", value: "shippo")
req.addParam(name: "tracking_number", value: "SHIPPO_TRANSIT")
req.addParam(name: "metadata", value: "Order 000123")
req.addHeader(name: "Authorization", value: "ShippoToken <API_TOKEN>")
let resp = CkoHttpResponse()!
success = http.httpReq(url: "https://api.goshippo.com/tracks/", request: req, response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
let sbResponseBody = CkoStringBuilder()!
resp.getBodySb(sb: sbResponseBody)
let jResp = CkoJsonObject()!
jResp.loadSb(sb: 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.")
return
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "messages": [
// ],
// "carrier": "shippo",
// "tracking_number": "SHIPPO_TRANSIT",
// "address_from": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// },
// "address_to": {
// "city": "Chicago",
// "state": "IL",
// "zip": "60611",
// "country": "US"
// },
// "eta": "2019-07-07T17:07:44.989Z",
// "original_eta": "2019-07-07T17:07:44.989Z",
// "servicelevel": {
// "token": "shippo_priority",
// "name": "Priority Mail"
// },
// "metadata": "Shippo test tracking",
// "tracking_status": {
// "object_created": "2019-07-04T17:07:45.003Z",
// "object_updated": null,
// "object_id": "ee35fb56f5d04021b36168abedc04573",
// "status": "TRANSIT",
// "status_details": "Your shipment has departed from the origin.",
// "status_date": "2019-07-03T15:02:45.003Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// },
// "tracking_history": [
// {
// "object_created": "2019-07-04T17:07:45.005Z",
// "object_updated": null,
// "object_id": "2121a59f53ed42e0ae0436f636179156",
// "status": "UNKNOWN",
// "status_details": "The carrier has received the electronic shipment information.",
// "status_date": "2019-07-02T12:57:45.005Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// },
// {
// "object_created": "2019-07-04T17:07:45.005Z",
// "object_updated": null,
// "object_id": "06f949db1a8245beaa28df264b368a76",
// "status": "TRANSIT",
// "status_details": "Your shipment has departed from the origin.",
// "status_date": "2019-07-03T15:02:45.005Z",
// "substatus": null,
// "location": {
// "city": "San Francisco",
// "state": "CA",
// "zip": "94103",
// "country": "US"
// }
// }
// ],
// "transaction": null,
// "test": true
// }
// 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_created: String?
var object_updated: String?
var object_id: String?
var status: String?
var status_details: String?
var status_date: String?
var substatus: String?
var locationCity: String?
var locationState: String?
var locationZip: String?
var locationCountry: String?
var carrier: String? = jResp.string(of: "carrier")
var tracking_number: String? = jResp.string(of: "tracking_number")
var address_fromCity: String? = jResp.string(of: "address_from.city")
var address_fromState: String? = jResp.string(of: "address_from.state")
var address_fromZip: String? = jResp.string(of: "address_from.zip")
var address_fromCountry: String? = jResp.string(of: "address_from.country")
var address_toCity: String? = jResp.string(of: "address_to.city")
var address_toState: String? = jResp.string(of: "address_to.state")
var address_toZip: String? = jResp.string(of: "address_to.zip")
var address_toCountry: String? = jResp.string(of: "address_to.country")
var eta: String? = jResp.string(of: "eta")
var original_eta: String? = jResp.string(of: "original_eta")
var servicelevelToken: String? = jResp.string(of: "servicelevel.token")
var servicelevelName: String? = jResp.string(of: "servicelevel.name")
var metadata: String? = jResp.string(of: "metadata")
var tracking_statusObject_created: String? = jResp.string(of: "tracking_status.object_created")
var tracking_statusObject_updated: String? = jResp.string(of: "tracking_status.object_updated")
var tracking_statusObject_id: String? = jResp.string(of: "tracking_status.object_id")
var tracking_statusStatus: String? = jResp.string(of: "tracking_status.status")
var tracking_statusStatus_details: String? = jResp.string(of: "tracking_status.status_details")
var tracking_statusStatus_date: String? = jResp.string(of: "tracking_status.status_date")
var tracking_statusSubstatus: String? = jResp.string(of: "tracking_status.substatus")
var tracking_statusLocationCity: String? = jResp.string(of: "tracking_status.location.city")
var tracking_statusLocationState: String? = jResp.string(of: "tracking_status.location.state")
var tracking_statusLocationZip: String? = jResp.string(of: "tracking_status.location.zip")
var tracking_statusLocationCountry: String? = jResp.string(of: "tracking_status.location.country")
var transaction: String? = jResp.string(of: "transaction")
var test: Bool = jResp.bool(of: "test")
var i: Int = 0
var count_i: Int = jResp.size(ofArray: "messages").intValue
while i < count_i {
jResp.i = i
i = i + 1
}
i = 0
count_i = jResp.size(ofArray: "tracking_history").intValue
while i < count_i {
jResp.i = i
object_created = jResp.string(of: "tracking_history[i].object_created")
object_updated = jResp.string(of: "tracking_history[i].object_updated")
object_id = jResp.string(of: "tracking_history[i].object_id")
status = jResp.string(of: "tracking_history[i].status")
status_details = jResp.string(of: "tracking_history[i].status_details")
status_date = jResp.string(of: "tracking_history[i].status_date")
substatus = jResp.string(of: "tracking_history[i].substatus")
locationCity = jResp.string(of: "tracking_history[i].location.city")
locationState = jResp.string(of: "tracking_history[i].location.state")
locationZip = jResp.string(of: "tracking_history[i].location.zip")
locationCountry = jResp.string(of: "tracking_history[i].location.country")
i = i + 1
}
}