Sample code for 30+ languages & platforms
Perl

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

Perl
use strict;
use warnings;
use Chilkat2;

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

my $http = Chilkat2::Http->new;

# This is the testing endpoint for the tracking API:
my $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.
my $json = Chilkat2::JsonObject->new;
$json->UpdateString('UPSSecurity.UsernameToken.Username', 'UPS_USERNAME');
$json->UpdateString('UPSSecurity.UsernameToken.Password', 'UPS_PASSWORD');
$json->UpdateString('UPSSecurity.ServiceAccessToken.AccessLicenseNumber', 'UPS_ACCESS_KEY');
# Request all activity...
$json->UpdateString('TrackRequest.Request.RequestOption', 'activity');
$json->UpdateString('TrackRequest.Request.TransactionReference.CustomerContext', 'Your Test Case Summary Description');
$json->UpdateString('TrackRequest.InquiryNumber', '1Z12345E0205271688');

my $sb = Chilkat2::StringBuilder->new;
my $resp = Chilkat2::HttpResponse->new;
my $success = $http->HttpJson('POST', $url, $json, 'application/json', $resp);
if (!$success) {
    print $http->LastErrorText, "\n";
    exit;
}

print 'status = ', $resp->StatusCode, "\n";

# A 200 response status indicate success.
if ($resp->StatusCode != 200) {
    print $resp->BodyStr, "\n";
    print "Failed.\n";
    exit;
}

$json->Load($resp->BodyStr);
$json->EmitCompact(0);
print $json->Emit(), "\n";

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

my $statusCode = $json->StringOf('TrackResponse.Response.ResponseStatus.Code');
my $statusDescription = $json->StringOf('TrackResponse.Response.ResponseStatus.Description');
print 'statusCode: ', $statusCode, "\n";
print 'statusDescription', $statusDescription, "\n";
my $customerContext = $json->StringOf('TrackResponse.Response.TransactionReference.CustomerContext');
my $inquiryNumberCode = $json->StringOf('TrackResponse.Shipment.InquiryNumber.Code');
my $inquiryNumberDescription = $json->StringOf('TrackResponse.Shipment.InquiryNumber.Description');
my $inquiryNumberValue = $json->StringOf('TrackResponse.Shipment.InquiryNumber.Value');
my $shipmentTypeCode = $json->StringOf('TrackResponse.Shipment.ShipmentType.Code');
my $shipmentTypeDescription = $json->StringOf('TrackResponse.Shipment.ShipmentType.Description');
my $shipperNumber = $json->StringOf('TrackResponse.Shipment.ShipperNumber');
my $serviceCode = $json->StringOf('TrackResponse.Shipment.Service.Code');
my $serviceDescription = $json->StringOf('TrackResponse.Shipment.Service.Description');
my $referenceNumberCode = $json->StringOf('TrackResponse.Shipment.ReferenceNumber.Code');
my $referenceNumberValue = $json->StringOf('TrackResponse.Shipment.ReferenceNumber.Value');
my $pickupDate = $json->StringOf('TrackResponse.Shipment.PickupDate');
my $trackingNumber = $json->StringOf('TrackResponse.Shipment.Package.TrackingNumber');
my $unitOfMeasurementCode = $json->StringOf('TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code');
my $weight = $json->StringOf('TrackResponse.Shipment.Package.PackageWeight.Weight');
my $disclaimer = $json->StringOf('TrackResponse.Disclaimer');

my $i = 0;
my $activityCount = $json->SizeOfArray('TrackResponse.Shipment.Package.Activity');
print 'activityCount: ', $activityCount, "\n";

while ($i < $activityCount) {
    print '-- activity ', $i, "\n";
    $json->I($i);
    if ($json->HasMember('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City')) {
        my $city = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City');
        print 'city: ', $city, "\n";
        my $provinceCode = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode');
        my $postalCode = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode');
        my $countryCode = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode');
    }

    my $locationCode = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code');
    my $locationDescription = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description');
    my $locationSignedForByName = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName');

    my $activityStatusType = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].Status.Type');
    print 'activityStatusType: ', $activityStatusType, "\n";
    my $activityStatusDescription = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].Status.Description');
    print 'activityStatusDescription: ', $activityStatusDescription, "\n";
    my $activityStatusCode = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].Status.Code');
    print 'activityStatusCode: ', $activityStatusCode, "\n";

    my $activityDate = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].Date');
    my $activityTime = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].Time');

    if ($json->HasMember('TrackResponse.Shipment.Package.Activity[i].Document')) {
        my $typeCode = $json->IntOf('TrackResponse.Shipment.Package.Activity[i].Document.Type.Code');
        my $typeDescription = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].Document.Type.Description');
        my $documentContent = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].Document.Content');
        my $documentFormatCode = $json->StringOf('TrackResponse.Shipment.Package.Activity[i].Document.Format.Code');
        # Format description would be something like "GIF" for a signature image.
        my $documentFormatDescription = $json->StringOf('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.
            my $sbImagePath = Chilkat2::StringBuilder->new;
            $sbImagePath->Append('qa_output/sig_');
            $sbImagePath->Append($trackingNumber);
            $sbImagePath->Append('.');
            $sbImagePath->Append($documentFormatDescription);
            my $imageData = Chilkat2::BinData->new;
            $success = $imageData->AppendEncoded($documentContent, 'base64');
            # Write to "qa_output/sig_1Z12345E0205271688.GIF"
            $success = $imageData->WriteFile($sbImagePath->GetAsString());
        }
    }

    $i = $i + 1;
}

$i = 0;
my $refnumCount = $json->SizeOfArray('TrackResponse.Shipment.Package.ReferenceNumber');
while ($i < $refnumCount) {
    $json->I($i);
    my $refnumCode = $json->StringOf('TrackResponse.Shipment.Package.ReferenceNumber[i].Code');
    my $refnumValue = $json->StringOf('TrackResponse.Shipment.Package.ReferenceNumber[i].Value');
    $i = $i + 1;
}

print "Success.\n";