React Native
React Native
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 React Native Downloads
import { BinData, Http, HttpResponse, JsonObject, StringBuilder } from '@chilkat/react-native'
async function chilkatExample() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const http = new Http();
// This is the testing endpoint for the tracking API:
const 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.
const json = new JsonObject();
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');
const sb = new StringBuilder();
const resp = new HttpResponse();
try {
await http.httpJsonAsync('POST', url, json, 'application/json', resp);
} catch {
console.log(http.lastErrorText);
return;
}
console.log(`status = ${resp.statusCode}`);
// A 200 response status indicate success.
if (resp.statusCode !== 200) {
console.log(resp.bodyStr);
console.log('Failed.');
return;
}
json.load(resp.bodyStr);
json.emitCompact = false;
console.log(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.
const statusCode = json.stringOf('TrackResponse.Response.ResponseStatus.Code');
const statusDescription = json.stringOf('TrackResponse.Response.ResponseStatus.Description');
console.log(`statusCode: ${statusCode}`);
console.log(`statusDescription${statusDescription}`);
const customerContext = json.stringOf('TrackResponse.Response.TransactionReference.CustomerContext');
const inquiryNumberCode = json.stringOf('TrackResponse.Shipment.InquiryNumber.Code');
const inquiryNumberDescription = json.stringOf('TrackResponse.Shipment.InquiryNumber.Description');
const inquiryNumberValue = json.stringOf('TrackResponse.Shipment.InquiryNumber.Value');
const shipmentTypeCode = json.stringOf('TrackResponse.Shipment.ShipmentType.Code');
const shipmentTypeDescription = json.stringOf('TrackResponse.Shipment.ShipmentType.Description');
const shipperNumber = json.stringOf('TrackResponse.Shipment.ShipperNumber');
const serviceCode = json.stringOf('TrackResponse.Shipment.Service.Code');
const serviceDescription = json.stringOf('TrackResponse.Shipment.Service.Description');
const referenceNumberCode = json.stringOf('TrackResponse.Shipment.ReferenceNumber.Code');
const referenceNumberValue = json.stringOf('TrackResponse.Shipment.ReferenceNumber.Value');
const pickupDate = json.stringOf('TrackResponse.Shipment.PickupDate');
const trackingNumber = json.stringOf('TrackResponse.Shipment.Package.TrackingNumber');
const unitOfMeasurementCode = json.stringOf('TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code');
const weight = json.stringOf('TrackResponse.Shipment.Package.PackageWeight.Weight');
const disclaimer = json.stringOf('TrackResponse.Disclaimer');
let i = 0;
const activityCount = json.sizeOfArray('TrackResponse.Shipment.Package.Activity');
console.log(`activityCount: ${activityCount}`);
while (i < activityCount) {
console.log(`-- activity ${i}`);
json.i = i;
if (json.hasMember('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City')) {
const city = json.stringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City');
console.log(`city: ${city}`);
const provinceCode = json.stringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode');
const postalCode = json.stringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode');
const countryCode = json.stringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode');
}
const locationCode = json.stringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code');
const locationDescription = json.stringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description');
const locationSignedForByName = json.stringOf('TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName');
const activityStatusType = json.stringOf('TrackResponse.Shipment.Package.Activity[i].Status.Type');
console.log(`activityStatusType: ${activityStatusType}`);
const activityStatusDescription = json.stringOf('TrackResponse.Shipment.Package.Activity[i].Status.Description');
console.log(`activityStatusDescription: ${activityStatusDescription}`);
const activityStatusCode = json.stringOf('TrackResponse.Shipment.Package.Activity[i].Status.Code');
console.log(`activityStatusCode: ${activityStatusCode}`);
const activityDate = json.stringOf('TrackResponse.Shipment.Package.Activity[i].Date');
const activityTime = json.stringOf('TrackResponse.Shipment.Package.Activity[i].Time');
if (json.hasMember('TrackResponse.Shipment.Package.Activity[i].Document')) {
const typeCode = json.intOf('TrackResponse.Shipment.Package.Activity[i].Document.Type.Code');
const typeDescription = json.stringOf('TrackResponse.Shipment.Package.Activity[i].Document.Type.Description');
const documentContent = json.stringOf('TrackResponse.Shipment.Package.Activity[i].Document.Content');
const documentFormatCode = json.stringOf('TrackResponse.Shipment.Package.Activity[i].Document.Format.Code');
// Format description would be something like "GIF" for a signature image.
const 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.
const sbImagePath = new StringBuilder();
sbImagePath.append('qa_output/sig_');
sbImagePath.append(trackingNumber);
sbImagePath.append('.');
sbImagePath.append(documentFormatDescription);
const imageData = new BinData();
imageData.appendEncoded(documentContent, 'base64');
// Write to "qa_output/sig_1Z12345E0205271688.GIF"
imageData.writeFile(sbImagePath.getAsString());
}
}
i++;
}
i = 0;
const refnumCount = json.sizeOfArray('TrackResponse.Shipment.Package.ReferenceNumber');
while (i < refnumCount) {
json.i = i;
const refnumCode = json.stringOf('TrackResponse.Shipment.Package.ReferenceNumber[i].Code');
const refnumValue = json.stringOf('TrackResponse.Shipment.Package.ReferenceNumber[i].Value');
i++;
}
console.log('Success.');
}