Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkBinData.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires 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

    ; This is the testing endpoint for the tracking API:
    url.s = "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.
    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"UPSSecurity.UsernameToken.Username","UPS_USERNAME")
    CkJsonObject::ckUpdateString(json,"UPSSecurity.UsernameToken.Password","UPS_PASSWORD")
    CkJsonObject::ckUpdateString(json,"UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY")
    ; Request all activity...
    CkJsonObject::ckUpdateString(json,"TrackRequest.Request.RequestOption","activity")
    CkJsonObject::ckUpdateString(json,"TrackRequest.Request.TransactionReference.CustomerContext","Your Test Case Summary Description")
    CkJsonObject::ckUpdateString(json,"TrackRequest.InquiryNumber","1Z12345E0205271688")

    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"POST",url,json,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sb)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    Debug "status = " + Str(CkHttpResponse::ckStatusCode(resp))

    ; A 200 response status indicate success.
    If CkHttpResponse::ckStatusCode(resp) <> 200
        Debug CkHttpResponse::ckBodyStr(resp)
        Debug "Failed."
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sb)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    CkJsonObject::ckLoad(json,CkHttpResponse::ckBodyStr(resp))
    CkJsonObject::setCkEmitCompact(json, 0)
    Debug CkJsonObject::ckEmit(json)

    ; {
    ;   "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.

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

    i.i = 0
    activityCount.i = CkJsonObject::ckSizeOfArray(json,"TrackResponse.Shipment.Package.Activity")
    Debug "activityCount: " + Str(activityCount)

    While i < activityCount
        Debug "-- activity " + Str(i)
        CkJsonObject::setCkI(json, i)
        If CkJsonObject::ckHasMember(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") = 1
            city.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City")
            Debug "city: " + city
            provinceCode.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode")
            postalCode.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode")
            countryCode.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode")
        EndIf

        locationCode.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code")
        locationDescription.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description")
        locationSignedForByName.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName")

        activityStatusType.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Status.Type")
        Debug "activityStatusType: " + activityStatusType
        activityStatusDescription.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Status.Description")
        Debug "activityStatusDescription: " + activityStatusDescription
        activityStatusCode.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Status.Code")
        Debug "activityStatusCode: " + activityStatusCode

        activityDate.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Date")
        activityTime.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Time")

        If CkJsonObject::ckHasMember(json,"TrackResponse.Shipment.Package.Activity[i].Document") = 1
            typeCode.i = CkJsonObject::ckIntOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Type.Code")
            typeDescription.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Type.Description")
            documentContent.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Content")
            documentFormatCode.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Format.Code")
            ; Format description would be something like "GIF" for a signature image.
            documentFormatDescription.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Format.Description")

            ; 01 - Signature Image
            ; 02 - Delivery Receipt
            ; 03 - Free Astray
            ; 04 - POD
            If typeCode = 1
                ; We have a signature image.  Get the image data and save to a file.
                sbImagePath.i = CkStringBuilder::ckCreate()
                If sbImagePath.i = 0
                    Debug "Failed to create object."
                    ProcedureReturn
                EndIf

                CkStringBuilder::ckAppend(sbImagePath,"qa_output/sig_")
                CkStringBuilder::ckAppend(sbImagePath,trackingNumber)
                CkStringBuilder::ckAppend(sbImagePath,".")
                CkStringBuilder::ckAppend(sbImagePath,documentFormatDescription)
                imageData.i = CkBinData::ckCreate()
                If imageData.i = 0
                    Debug "Failed to create object."
                    ProcedureReturn
                EndIf

                success = CkBinData::ckAppendEncoded(imageData,documentContent,"base64")
                ; Write to "qa_output/sig_1Z12345E0205271688.GIF"
                success = CkBinData::ckWriteFile(imageData,CkStringBuilder::ckGetAsString(sbImagePath))
            EndIf

        EndIf

        i = i + 1
    Wend

    i = 0
    refnumCount.i = CkJsonObject::ckSizeOfArray(json,"TrackResponse.Shipment.Package.ReferenceNumber")
    While i < refnumCount
        CkJsonObject::setCkI(json, i)
        refnumCode.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.ReferenceNumber[i].Code")
        refnumValue.s = CkJsonObject::ckStringOf(json,"TrackResponse.Shipment.Package.ReferenceNumber[i].Value")
        i = i + 1
    Wend

    Debug "Success."


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sb)
    CkHttpResponse::ckDispose(resp)
    CkStringBuilder::ckDispose(sbImagePath)
    CkBinData::ckDispose(imageData)


    ProcedureReturn
EndProcedure