PureBasic
PureBasic
Shippo Adding Metadata
See more Shippo Examples
Demonstrates how to add metadata to the tracking request through a POST request.Chilkat PureBasic Downloads
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttpRequest.pb"
Procedure ChilkatExample()
success.i = 0
; 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
; 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"
req.i = CkHttpRequest::ckCreate()
If req.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHttpRequest::setCkHttpVerb(req, "POST")
CkHttpRequest::setCkPath(req, "/tracks/")
CkHttpRequest::setCkContentType(req, "application/x-www-form-urlencoded")
CkHttpRequest::ckAddParam(req,"carrier","shippo")
CkHttpRequest::ckAddParam(req,"tracking_number","SHIPPO_TRANSIT")
CkHttpRequest::ckAddParam(req,"metadata","Order 000123")
CkHttpRequest::ckAddHeader(req,"Authorization","ShippoToken <API_TOKEN>")
resp.i = CkHttpResponse::ckCreate()
If resp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckHttpReq(http,"https://api.goshippo.com/tracks/",req,resp)
If success = 0
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkHttpResponse::ckDispose(resp)
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."
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndIf
; 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
object_created.s
object_updated.s
object_id.s
status.s
status_details.s
status_date.s
substatus.s
locationCity.s
locationState.s
locationZip.s
locationCountry.s
carrier.s = CkJsonObject::ckStringOf(jResp,"carrier")
tracking_number.s = CkJsonObject::ckStringOf(jResp,"tracking_number")
address_fromCity.s = CkJsonObject::ckStringOf(jResp,"address_from.city")
address_fromState.s = CkJsonObject::ckStringOf(jResp,"address_from.state")
address_fromZip.s = CkJsonObject::ckStringOf(jResp,"address_from.zip")
address_fromCountry.s = CkJsonObject::ckStringOf(jResp,"address_from.country")
address_toCity.s = CkJsonObject::ckStringOf(jResp,"address_to.city")
address_toState.s = CkJsonObject::ckStringOf(jResp,"address_to.state")
address_toZip.s = CkJsonObject::ckStringOf(jResp,"address_to.zip")
address_toCountry.s = CkJsonObject::ckStringOf(jResp,"address_to.country")
eta.s = CkJsonObject::ckStringOf(jResp,"eta")
original_eta.s = CkJsonObject::ckStringOf(jResp,"original_eta")
servicelevelToken.s = CkJsonObject::ckStringOf(jResp,"servicelevel.token")
servicelevelName.s = CkJsonObject::ckStringOf(jResp,"servicelevel.name")
metadata.s = CkJsonObject::ckStringOf(jResp,"metadata")
tracking_statusObject_created.s = CkJsonObject::ckStringOf(jResp,"tracking_status.object_created")
tracking_statusObject_updated.s = CkJsonObject::ckStringOf(jResp,"tracking_status.object_updated")
tracking_statusObject_id.s = CkJsonObject::ckStringOf(jResp,"tracking_status.object_id")
tracking_statusStatus.s = CkJsonObject::ckStringOf(jResp,"tracking_status.status")
tracking_statusStatus_details.s = CkJsonObject::ckStringOf(jResp,"tracking_status.status_details")
tracking_statusStatus_date.s = CkJsonObject::ckStringOf(jResp,"tracking_status.status_date")
tracking_statusSubstatus.s = CkJsonObject::ckStringOf(jResp,"tracking_status.substatus")
tracking_statusLocationCity.s = CkJsonObject::ckStringOf(jResp,"tracking_status.location.city")
tracking_statusLocationState.s = CkJsonObject::ckStringOf(jResp,"tracking_status.location.state")
tracking_statusLocationZip.s = CkJsonObject::ckStringOf(jResp,"tracking_status.location.zip")
tracking_statusLocationCountry.s = CkJsonObject::ckStringOf(jResp,"tracking_status.location.country")
transaction.s = CkJsonObject::ckStringOf(jResp,"transaction")
test.i = CkJsonObject::ckBoolOf(jResp,"test")
i.i = 0
count_i.i = CkJsonObject::ckSizeOfArray(jResp,"messages")
While i < count_i
CkJsonObject::setCkI(jResp, i)
i = i + 1
Wend
i = 0
count_i = CkJsonObject::ckSizeOfArray(jResp,"tracking_history")
While i < count_i
CkJsonObject::setCkI(jResp, i)
object_created = CkJsonObject::ckStringOf(jResp,"tracking_history[i].object_created")
object_updated = CkJsonObject::ckStringOf(jResp,"tracking_history[i].object_updated")
object_id = CkJsonObject::ckStringOf(jResp,"tracking_history[i].object_id")
status = CkJsonObject::ckStringOf(jResp,"tracking_history[i].status")
status_details = CkJsonObject::ckStringOf(jResp,"tracking_history[i].status_details")
status_date = CkJsonObject::ckStringOf(jResp,"tracking_history[i].status_date")
substatus = CkJsonObject::ckStringOf(jResp,"tracking_history[i].substatus")
locationCity = CkJsonObject::ckStringOf(jResp,"tracking_history[i].location.city")
locationState = CkJsonObject::ckStringOf(jResp,"tracking_history[i].location.state")
locationZip = CkJsonObject::ckStringOf(jResp,"tracking_history[i].location.zip")
locationCountry = CkJsonObject::ckStringOf(jResp,"tracking_history[i].location.country")
i = i + 1
Wend
CkHttp::ckDispose(http)
CkHttpRequest::ckDispose(req)
CkHttpResponse::ckDispose(resp)
CkStringBuilder::ckDispose(sbResponseBody)
CkJsonObject::ckDispose(jResp)
ProcedureReturn
EndProcedure