Lua
Lua
Shippo Adding Metadata
See more Shippo Examples
Demonstrates how to add metadata to the tracking request through a POST request.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- 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"
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/tracks/"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("carrier", "shippo")
req:add_param("tracking_number", "SHIPPO_TRANSIT")
req:add_param("metadata", "Order 000123")
req:add_header("Authorization", "ShippoToken <API_TOKEN>")
local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://api.goshippo.com/tracks/", req, resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- 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
local object_created = ""
local object_updated = ""
local object_id = ""
local status = ""
local status_details = ""
local status_date = ""
local substatus = ""
local locationCity = ""
local locationState = ""
local locationZip = ""
local locationCountry = ""
local carrier = jResp:string_of("carrier")
local tracking_number = jResp:string_of("tracking_number")
local address_fromCity = jResp:string_of("address_from.city")
local address_fromState = jResp:string_of("address_from.state")
local address_fromZip = jResp:string_of("address_from.zip")
local address_fromCountry = jResp:string_of("address_from.country")
local address_toCity = jResp:string_of("address_to.city")
local address_toState = jResp:string_of("address_to.state")
local address_toZip = jResp:string_of("address_to.zip")
local address_toCountry = jResp:string_of("address_to.country")
local eta = jResp:string_of("eta")
local original_eta = jResp:string_of("original_eta")
local servicelevelToken = jResp:string_of("servicelevel.token")
local servicelevelName = jResp:string_of("servicelevel.name")
local metadata = jResp:string_of("metadata")
local tracking_statusObject_created = jResp:string_of("tracking_status.object_created")
local tracking_statusObject_updated = jResp:string_of("tracking_status.object_updated")
local tracking_statusObject_id = jResp:string_of("tracking_status.object_id")
local tracking_statusStatus = jResp:string_of("tracking_status.status")
local tracking_statusStatus_details = jResp:string_of("tracking_status.status_details")
local tracking_statusStatus_date = jResp:string_of("tracking_status.status_date")
local tracking_statusSubstatus = jResp:string_of("tracking_status.substatus")
local tracking_statusLocationCity = jResp:string_of("tracking_status.location.city")
local tracking_statusLocationState = jResp:string_of("tracking_status.location.state")
local tracking_statusLocationZip = jResp:string_of("tracking_status.location.zip")
local tracking_statusLocationCountry = jResp:string_of("tracking_status.location.country")
local transaction = jResp:string_of("transaction")
local test = jResp:bool_of("test")
local i = 0
local count_i = jResp:size_of_array("messages")
while i < count_i do
jResp.i = i
i = i + 1
end
i = 0
count_i = jResp:size_of_array("tracking_history")
while i < count_i do
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
end