Sample code for 30+ languages & platforms
Lua

UPS Tracking API

See more HTTP Misc Examples

Demonstrates making a call to the UPS tracking REST API. Parses the tracking response and extracts the base64 signature image to a gif file.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local http = chilkat.Http.new()

-- This is the testing endpoint for the tracking API:
local url = "https://wwwcie.ups.com/rest/Track"

-- Send an HTTP request with the following JSON body:

-- {
--   "UPSSecurity": {
--     "UsernameToken": {
--       "Username": "Your Username",
--       "Password": "Your Password"
--     },
--     "ServiceAccessToken": {
--       "AccessLicenseNumber": "Your Access License Number"
--     }
--   },
--   "TrackRequest": {
--     "Request": {
--       "RequestOption": "1",
--       "TransactionReference": {
--         "CustomerContext": "Your Test Case Summary Description"
--       }
--     },
--     "InquiryNumber": "YourTrackingNumber"
--   }
-- }
--

-- Build the above JSON.
local json = chilkat.JsonObject.new()
json:update_string("UPSSecurity.UsernameToken.Username", "UPS_USERNAME")
json:update_string("UPSSecurity.UsernameToken.Password", "UPS_PASSWORD")
json:update_string("UPSSecurity.ServiceAccessToken.AccessLicenseNumber", "UPS_ACCESS_KEY")
-- Request all activity...
json:update_string("TrackRequest.Request.RequestOption", "activity")
json:update_string("TrackRequest.Request.TransactionReference.CustomerContext", "Your Test Case Summary Description")
json:update_string("TrackRequest.InquiryNumber", "1Z12345E0205271688")

local sb = chilkat.StringBuilder.new()
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", url, json, "application/json", resp)
if not success then
    print(http.last_error_text)
    return
end

print("status = " .. resp.status_code)

-- A 200 response status indicate success.
if resp.status_code ~= 200 then
    print(resp.body_str)
    print("Failed.")
    return
end

json:load(resp.body_str)
json.emit_compact = false
print(json:emit())

-- {
--   "TrackResponse": {
--     "Response": {
--       "ResponseStatus": {
--         "Code": "1",
--         "Description": "Success"
--       },
--       "TransactionReference": {
--         "CustomerContext": "Your Test Case Summary Description"
--       }
--     },
--     "Shipment": {
--       "InquiryNumber": {
--         "Code": "01",
--         "Description": "ShipmentIdentificationNumber",
--         "Value": "1Z12345E0205271688"
--       },
--       "ShipmentType": {
--         "Code": "01",
--         "Description": "Small Package"
--       },
--       "ShipperNumber": "12345E",
--       "Service": {
--         "Code": "002",
--         "Description": "2ND DAY AIR"
--       },
--       "ReferenceNumber": {
--         "Code": "01",
--         "Value": "LINE4AND115"
--       },
--       "PickupDate": "19990608",
--       "Package": {
--         "TrackingNumber": "1Z12345E0205271688",
--         "Activity": [
--           {
--             "ActivityLocation": {
--               "Address": {
--                 "City": "ANYTOWN",
--                 "StateProvinceCode": "GA",
--
--                 "PostalCode": "30340",
--                 "CountryCode": "US"
--               },
--               "Code": "ML",
--               "Description": "BACK DOOR",
--               "SignedForByName": "JOHN DOE"
--             },
--             "Status": {
--               "Type": "D",
--               "Description": "DELIVERED",
--               "Code": "KM"
--             },
--             "Date": "19990610",
--             "Time": "120000",
--             "Document": {
--               "Type": {
--                 "Code": "01",
--                 "Description": "Signature Image"
--               },
--               "Content": "R0lGODdhoA ... JU9Y8RdHsRKLMVJ4MVDMREAAADs=",
--               "Format": {
--                 "Code": "01",
--                 "Description": "GIF"
--               }
--             }
--           },
--           {
--             "Status": {
--               "Type": "M",
--               "Description": "BILLING INFORMATION RECEIVED. SHIPMENT DATE PENDING.",
--               "Code": "MP"
--             },
--             "Date": "19990608",
--             "Time": "120000"
--           }
--         ],
--         "PackageWeight": {
--           "UnitOfMeasurement": {
--             "Code": "LBS"
--           },
--           "Weight": "5.00"
--         },
--         "ReferenceNumber": [
--           {
--             "Code": "01",
--             "Value": "LINE4AND115"
--           },
--           {
--             "Code": "08",
--             "Value": "LJ67Y5"
--           }
--         ]
--       }
--     },
--     "Disclaimer": "You are using UPS tracking service on customer integration test environment, please switch to UPS production environment once you finish the test. The URL is https://onlinetools.ups.com/webservices/Track"
--   }
-- }

-- Use the online tool at Generate JSON Parsing Code
-- to generate JSON parsing code.

local statusCode = json:string_of("TrackResponse.Response.ResponseStatus.Code")
local statusDescription = json:string_of("TrackResponse.Response.ResponseStatus.Description")
print("statusCode: " .. statusCode)
print("statusDescription" .. statusDescription)
local customerContext = json:string_of("TrackResponse.Response.TransactionReference.CustomerContext")
local inquiryNumberCode = json:string_of("TrackResponse.Shipment.InquiryNumber.Code")
local inquiryNumberDescription = json:string_of("TrackResponse.Shipment.InquiryNumber.Description")
local inquiryNumberValue = json:string_of("TrackResponse.Shipment.InquiryNumber.Value")
local shipmentTypeCode = json:string_of("TrackResponse.Shipment.ShipmentType.Code")
local shipmentTypeDescription = json:string_of("TrackResponse.Shipment.ShipmentType.Description")
local shipperNumber = json:string_of("TrackResponse.Shipment.ShipperNumber")
local serviceCode = json:string_of("TrackResponse.Shipment.Service.Code")
local serviceDescription = json:string_of("TrackResponse.Shipment.Service.Description")
local referenceNumberCode = json:string_of("TrackResponse.Shipment.ReferenceNumber.Code")
local referenceNumberValue = json:string_of("TrackResponse.Shipment.ReferenceNumber.Value")
local pickupDate = json:string_of("TrackResponse.Shipment.PickupDate")
local trackingNumber = json:string_of("TrackResponse.Shipment.Package.TrackingNumber")
local unitOfMeasurementCode = json:string_of("TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code")
local weight = json:string_of("TrackResponse.Shipment.Package.PackageWeight.Weight")
local disclaimer = json:string_of("TrackResponse.Disclaimer")

local i = 0
local activityCount = json:size_of_array("TrackResponse.Shipment.Package.Activity")
print("activityCount: " .. activityCount)

while i < activityCount do
    print("-- activity " .. i)
    json.i = i
    if json:has_member("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") then
        local city = json:string_of("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City")
        print("city: " .. city)
        local provinceCode = json:string_of("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode")
        local postalCode = json:string_of("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode")
        local countryCode = json:string_of("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode")
    end

    local locationCode = json:string_of("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code")
    local locationDescription = json:string_of("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description")
    local locationSignedForByName = json:string_of("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName")

    local activityStatusType = json:string_of("TrackResponse.Shipment.Package.Activity[i].Status.Type")
    print("activityStatusType: " .. activityStatusType)
    local activityStatusDescription = json:string_of("TrackResponse.Shipment.Package.Activity[i].Status.Description")
    print("activityStatusDescription: " .. activityStatusDescription)
    local activityStatusCode = json:string_of("TrackResponse.Shipment.Package.Activity[i].Status.Code")
    print("activityStatusCode: " .. activityStatusCode)

    local activityDate = json:string_of("TrackResponse.Shipment.Package.Activity[i].Date")
    local activityTime = json:string_of("TrackResponse.Shipment.Package.Activity[i].Time")

    if json:has_member("TrackResponse.Shipment.Package.Activity[i].Document") then
        local typeCode = json:int_of("TrackResponse.Shipment.Package.Activity[i].Document.Type.Code")
        local typeDescription = json:string_of("TrackResponse.Shipment.Package.Activity[i].Document.Type.Description")
        local documentContent = json:string_of("TrackResponse.Shipment.Package.Activity[i].Document.Content")
        local documentFormatCode = json:string_of("TrackResponse.Shipment.Package.Activity[i].Document.Format.Code")
        -- Format description would be something like "GIF" for a signature image.
        local documentFormatDescription = json:string_of("TrackResponse.Shipment.Package.Activity[i].Document.Format.Description")

        -- 01 - Signature Image
        -- 02 - Delivery Receipt
        -- 03 - Free Astray
        -- 04 - POD
        if typeCode == 1 then
            -- We have a signature image.  Get the image data and save to a file.
            local sbImagePath = chilkat.StringBuilder.new()
            sbImagePath:append("qa_output/sig_")
            sbImagePath:append(trackingNumber)
            sbImagePath:append(".")
            sbImagePath:append(documentFormatDescription)
            local imageData = chilkat.BinData.new()
            success = imageData:append_encoded(documentContent, "base64")
            -- Write to "qa_output/sig_1Z12345E0205271688.GIF"
            success = imageData:write_file(sbImagePath:get_as_string())
        end
    end

    i = i + 1
end

i = 0
local refnumCount = json:size_of_array("TrackResponse.Shipment.Package.ReferenceNumber")
while i < refnumCount do
    json.i = i
    local refnumCode = json:string_of("TrackResponse.Shipment.Package.ReferenceNumber[i].Code")
    local refnumValue = json:string_of("TrackResponse.Shipment.Package.ReferenceNumber[i].Value")
    i = i + 1
end

print("Success.")