Sample code for 30+ languages & platforms
Tcl

Shippo Adding Metadata

See more Shippo Examples

Demonstrates how to add metadata to the tracking request through a POST request.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set http [new_CkHttp]

# 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"

set req [new_CkHttpRequest]

CkHttpRequest_put_HttpVerb $req "POST"
CkHttpRequest_put_Path $req "/tracks/"
CkHttpRequest_put_ContentType $req "application/x-www-form-urlencoded"
CkHttpRequest_AddParam $req "carrier" "shippo"
CkHttpRequest_AddParam $req "tracking_number" "SHIPPO_TRANSIT"
CkHttpRequest_AddParam $req "metadata" "Order 000123"

CkHttpRequest_AddHeader $req "Authorization" "ShippoToken <API_TOKEN>"

set resp [new_CkHttpResponse]

set success [CkHttp_HttpReq $http "https://api.goshippo.com/tracks/" $req $resp]
if {$success == 0} then {
    puts [CkHttp_lastErrorText $http]
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    exit
}

set sbResponseBody [new_CkStringBuilder]

CkHttpResponse_GetBodySb $resp $sbResponseBody
set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody
CkJsonObject_put_EmitCompact $jResp 0

puts "Response Body:"
puts [CkJsonObject_emit $jResp]

set respStatusCode [CkHttpResponse_get_StatusCode $resp]
puts "Response Status Code = $respStatusCode"
if {$respStatusCode >= 400} then {
    puts "Response Header:"
    puts [CkHttpResponse_header $resp]
    puts "Failed."
    delete_CkHttp $http
    delete_CkHttpRequest $req
    delete_CkHttpResponse $resp
    delete_CkStringBuilder $sbResponseBody
    delete_CkJsonObject $jResp
    exit
}

# 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

set carrier [CkJsonObject_stringOf $jResp "carrier"]
set tracking_number [CkJsonObject_stringOf $jResp "tracking_number"]
set address_fromCity [CkJsonObject_stringOf $jResp "address_from.city"]
set address_fromState [CkJsonObject_stringOf $jResp "address_from.state"]
set address_fromZip [CkJsonObject_stringOf $jResp "address_from.zip"]
set address_fromCountry [CkJsonObject_stringOf $jResp "address_from.country"]
set address_toCity [CkJsonObject_stringOf $jResp "address_to.city"]
set address_toState [CkJsonObject_stringOf $jResp "address_to.state"]
set address_toZip [CkJsonObject_stringOf $jResp "address_to.zip"]
set address_toCountry [CkJsonObject_stringOf $jResp "address_to.country"]
set eta [CkJsonObject_stringOf $jResp "eta"]
set original_eta [CkJsonObject_stringOf $jResp "original_eta"]
set servicelevelToken [CkJsonObject_stringOf $jResp "servicelevel.token"]
set servicelevelName [CkJsonObject_stringOf $jResp "servicelevel.name"]
set metadata [CkJsonObject_stringOf $jResp "metadata"]
set tracking_statusObject_created [CkJsonObject_stringOf $jResp "tracking_status.object_created"]
set tracking_statusObject_updated [CkJsonObject_stringOf $jResp "tracking_status.object_updated"]
set tracking_statusObject_id [CkJsonObject_stringOf $jResp "tracking_status.object_id"]
set tracking_statusStatus [CkJsonObject_stringOf $jResp "tracking_status.status"]
set tracking_statusStatus_details [CkJsonObject_stringOf $jResp "tracking_status.status_details"]
set tracking_statusStatus_date [CkJsonObject_stringOf $jResp "tracking_status.status_date"]
set tracking_statusSubstatus [CkJsonObject_stringOf $jResp "tracking_status.substatus"]
set tracking_statusLocationCity [CkJsonObject_stringOf $jResp "tracking_status.location.city"]
set tracking_statusLocationState [CkJsonObject_stringOf $jResp "tracking_status.location.state"]
set tracking_statusLocationZip [CkJsonObject_stringOf $jResp "tracking_status.location.zip"]
set tracking_statusLocationCountry [CkJsonObject_stringOf $jResp "tracking_status.location.country"]
set transaction [CkJsonObject_stringOf $jResp "transaction"]
set test [CkJsonObject_BoolOf $jResp "test"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "messages"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "tracking_history"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set object_created [CkJsonObject_stringOf $jResp "tracking_history[i].object_created"]
    set object_updated [CkJsonObject_stringOf $jResp "tracking_history[i].object_updated"]
    set object_id [CkJsonObject_stringOf $jResp "tracking_history[i].object_id"]
    set status [CkJsonObject_stringOf $jResp "tracking_history[i].status"]
    set status_details [CkJsonObject_stringOf $jResp "tracking_history[i].status_details"]
    set status_date [CkJsonObject_stringOf $jResp "tracking_history[i].status_date"]
    set substatus [CkJsonObject_stringOf $jResp "tracking_history[i].substatus"]
    set locationCity [CkJsonObject_stringOf $jResp "tracking_history[i].location.city"]
    set locationState [CkJsonObject_stringOf $jResp "tracking_history[i].location.state"]
    set locationZip [CkJsonObject_stringOf $jResp "tracking_history[i].location.zip"]
    set locationCountry [CkJsonObject_stringOf $jResp "tracking_history[i].location.country"]
    set i [expr $i + 1]
}

delete_CkHttp $http
delete_CkHttpRequest $req
delete_CkHttpResponse $resp
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp