Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL lcUrl
LOCAL loJson
LOCAL loSb
LOCAL loResp
LOCAL lcStatusCode
LOCAL lcStatusDescription
LOCAL lcCustomerContext
LOCAL lcInquiryNumberCode
LOCAL lcInquiryNumberDescription
LOCAL lcInquiryNumberValue
LOCAL lcShipmentTypeCode
LOCAL lcShipmentTypeDescription
LOCAL lcShipperNumber
LOCAL lcServiceCode
LOCAL lcServiceDescription
LOCAL lcReferenceNumberCode
LOCAL lcReferenceNumberValue
LOCAL lcPickupDate
LOCAL lcTrackingNumber
LOCAL lcUnitOfMeasurementCode
LOCAL lcWeight
LOCAL lcDisclaimer
LOCAL i
LOCAL lnActivityCount
LOCAL lcCity
LOCAL lcProvinceCode
LOCAL lcPostalCode
LOCAL lcCountryCode
LOCAL lcLocationCode
LOCAL lcLocationDescription
LOCAL lcLocationSignedForByName
LOCAL lcActivityStatusType
LOCAL lcActivityStatusDescription
LOCAL lcActivityStatusCode
LOCAL lcActivityDate
LOCAL lcActivityTime
LOCAL lnTypeCode
LOCAL lcTypeDescription
LOCAL lcDocumentContent
LOCAL lcDocumentFormatCode
LOCAL lcDocumentFormatDescription
LOCAL loSbImagePath
LOCAL loImageData
LOCAL lnRefnumCount
LOCAL lcRefnumCode
LOCAL lcRefnumValue

lnSuccess = 0

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

loHttp = CreateObject('Chilkat.Http')

* This is the testing endpoint for the tracking API:
lcUrl = "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.
loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateString("UPSSecurity.UsernameToken.Username","UPS_USERNAME")
loJson.UpdateString("UPSSecurity.UsernameToken.Password","UPS_PASSWORD")
loJson.UpdateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY")
* Request all activity...
loJson.UpdateString("TrackRequest.Request.RequestOption","activity")
loJson.UpdateString("TrackRequest.Request.TransactionReference.CustomerContext","Your Test Case Summary Description")
loJson.UpdateString("TrackRequest.InquiryNumber","1Z12345E0205271688")

loSb = CreateObject('Chilkat.StringBuilder')
loResp = CreateObject('Chilkat.HttpResponse')
lnSuccess = loHttp.HttpJson("POST",lcUrl,loJson,"application/json",loResp)
IF (lnSuccess = 0) THEN
    ? loHttp.LastErrorText
    RELEASE loHttp
    RELEASE loJson
    RELEASE loSb
    RELEASE loResp
    CANCEL
ENDIF

? "status = " + STR(loResp.StatusCode)

* A 200 response status indicate success.
IF (loResp.StatusCode <> 200) THEN
    ? loResp.BodyStr
    ? "Failed."
    RELEASE loHttp
    RELEASE loJson
    RELEASE loSb
    RELEASE loResp
    CANCEL
ENDIF

loJson.Load(loResp.BodyStr)
loJson.EmitCompact = 0
? loJson.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.

lcStatusCode = loJson.StringOf("TrackResponse.Response.ResponseStatus.Code")
lcStatusDescription = loJson.StringOf("TrackResponse.Response.ResponseStatus.Description")
? "statusCode: " + lcStatusCode
? "statusDescription" + lcStatusDescription
lcCustomerContext = loJson.StringOf("TrackResponse.Response.TransactionReference.CustomerContext")
lcInquiryNumberCode = loJson.StringOf("TrackResponse.Shipment.InquiryNumber.Code")
lcInquiryNumberDescription = loJson.StringOf("TrackResponse.Shipment.InquiryNumber.Description")
lcInquiryNumberValue = loJson.StringOf("TrackResponse.Shipment.InquiryNumber.Value")
lcShipmentTypeCode = loJson.StringOf("TrackResponse.Shipment.ShipmentType.Code")
lcShipmentTypeDescription = loJson.StringOf("TrackResponse.Shipment.ShipmentType.Description")
lcShipperNumber = loJson.StringOf("TrackResponse.Shipment.ShipperNumber")
lcServiceCode = loJson.StringOf("TrackResponse.Shipment.Service.Code")
lcServiceDescription = loJson.StringOf("TrackResponse.Shipment.Service.Description")
lcReferenceNumberCode = loJson.StringOf("TrackResponse.Shipment.ReferenceNumber.Code")
lcReferenceNumberValue = loJson.StringOf("TrackResponse.Shipment.ReferenceNumber.Value")
lcPickupDate = loJson.StringOf("TrackResponse.Shipment.PickupDate")
lcTrackingNumber = loJson.StringOf("TrackResponse.Shipment.Package.TrackingNumber")
lcUnitOfMeasurementCode = loJson.StringOf("TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code")
lcWeight = loJson.StringOf("TrackResponse.Shipment.Package.PackageWeight.Weight")
lcDisclaimer = loJson.StringOf("TrackResponse.Disclaimer")

i = 0
lnActivityCount = loJson.SizeOfArray("TrackResponse.Shipment.Package.Activity")
? "activityCount: " + STR(lnActivityCount)

DO WHILE i < lnActivityCount
    ? "-- activity " + STR(i)
    loJson.I = i
    IF (loJson.HasMember("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") = 1) THEN
        lcCity = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City")
        ? "city: " + lcCity
        lcProvinceCode = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode")
        lcPostalCode = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode")
        lcCountryCode = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode")
    ENDIF

    lcLocationCode = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code")
    lcLocationDescription = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description")
    lcLocationSignedForByName = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName")

    lcActivityStatusType = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Type")
    ? "activityStatusType: " + lcActivityStatusType
    lcActivityStatusDescription = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Description")
    ? "activityStatusDescription: " + lcActivityStatusDescription
    lcActivityStatusCode = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Code")
    ? "activityStatusCode: " + lcActivityStatusCode

    lcActivityDate = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Date")
    lcActivityTime = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Time")

    IF (loJson.HasMember("TrackResponse.Shipment.Package.Activity[i].Document") = 1) THEN
        lnTypeCode = loJson.IntOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Code")
        lcTypeDescription = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Description")
        lcDocumentContent = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Content")
        lcDocumentFormatCode = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Code")
        * Format description would be something like "GIF" for a signature image.
        lcDocumentFormatDescription = loJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Description")

        * 01 - Signature Image
        * 02 - Delivery Receipt
        * 03 - Free Astray
        * 04 - POD
        IF (lnTypeCode = 1) THEN
            * We have a signature image.  Get the image data and save to a file.
            loSbImagePath = CreateObject('Chilkat.StringBuilder')
            loSbImagePath.Append("qa_output/sig_")
            loSbImagePath.Append(lcTrackingNumber)
            loSbImagePath.Append(".")
            loSbImagePath.Append(lcDocumentFormatDescription)
            loImageData = CreateObject('Chilkat.BinData')
            lnSuccess = loImageData.AppendEncoded(lcDocumentContent,"base64")
            * Write to "qa_output/sig_1Z12345E0205271688.GIF"
            lnSuccess = loImageData.WriteFile(loSbImagePath.GetAsString())
        ENDIF

    ENDIF

    i = i + 1
ENDDO

i = 0
lnRefnumCount = loJson.SizeOfArray("TrackResponse.Shipment.Package.ReferenceNumber")
DO WHILE i < lnRefnumCount
    loJson.I = i
    lcRefnumCode = loJson.StringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Code")
    lcRefnumValue = loJson.StringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Value")
    i = i + 1
ENDDO

? "Success."

RELEASE loHttp
RELEASE loJson
RELEASE loSb
RELEASE loResp
RELEASE loSbImagePath
RELEASE loImageData