Unicode C++
Unicode C++
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 Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
#include <CkHttpResponseW.h>
#include <CkBinDataW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttpW http;
// This is the testing endpoint for the tracking API:
const wchar_t *url = L"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.
CkJsonObjectW json;
json.UpdateString(L"UPSSecurity.UsernameToken.Username",L"UPS_USERNAME");
json.UpdateString(L"UPSSecurity.UsernameToken.Password",L"UPS_PASSWORD");
json.UpdateString(L"UPSSecurity.ServiceAccessToken.AccessLicenseNumber",L"UPS_ACCESS_KEY");
// Request all activity...
json.UpdateString(L"TrackRequest.Request.RequestOption",L"activity");
json.UpdateString(L"TrackRequest.Request.TransactionReference.CustomerContext",L"Your Test Case Summary Description");
json.UpdateString(L"TrackRequest.InquiryNumber",L"1Z12345E0205271688");
CkStringBuilderW sb;
CkHttpResponseW resp;
success = http.HttpJson(L"POST",url,json,L"application/json",resp);
if (success == false) {
wprintf(L"%s\n",http.lastErrorText());
return;
}
wprintf(L"status = %d\n",resp.get_StatusCode());
// A 200 response status indicate success.
if (resp.get_StatusCode() != 200) {
wprintf(L"%s\n",resp.bodyStr());
wprintf(L"Failed.\n");
return;
}
json.Load(resp.bodyStr());
json.put_EmitCompact(false);
wprintf(L"%s\n",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 wchar_t *statusCode = json.stringOf(L"TrackResponse.Response.ResponseStatus.Code");
const wchar_t *statusDescription = json.stringOf(L"TrackResponse.Response.ResponseStatus.Description");
wprintf(L"statusCode: %s\n",statusCode);
wprintf(L"statusDescription%s\n",statusDescription);
const wchar_t *customerContext = json.stringOf(L"TrackResponse.Response.TransactionReference.CustomerContext");
const wchar_t *inquiryNumberCode = json.stringOf(L"TrackResponse.Shipment.InquiryNumber.Code");
const wchar_t *inquiryNumberDescription = json.stringOf(L"TrackResponse.Shipment.InquiryNumber.Description");
const wchar_t *inquiryNumberValue = json.stringOf(L"TrackResponse.Shipment.InquiryNumber.Value");
const wchar_t *shipmentTypeCode = json.stringOf(L"TrackResponse.Shipment.ShipmentType.Code");
const wchar_t *shipmentTypeDescription = json.stringOf(L"TrackResponse.Shipment.ShipmentType.Description");
const wchar_t *shipperNumber = json.stringOf(L"TrackResponse.Shipment.ShipperNumber");
const wchar_t *serviceCode = json.stringOf(L"TrackResponse.Shipment.Service.Code");
const wchar_t *serviceDescription = json.stringOf(L"TrackResponse.Shipment.Service.Description");
const wchar_t *referenceNumberCode = json.stringOf(L"TrackResponse.Shipment.ReferenceNumber.Code");
const wchar_t *referenceNumberValue = json.stringOf(L"TrackResponse.Shipment.ReferenceNumber.Value");
const wchar_t *pickupDate = json.stringOf(L"TrackResponse.Shipment.PickupDate");
const wchar_t *trackingNumber = json.stringOf(L"TrackResponse.Shipment.Package.TrackingNumber");
const wchar_t *unitOfMeasurementCode = json.stringOf(L"TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code");
const wchar_t *weight = json.stringOf(L"TrackResponse.Shipment.Package.PackageWeight.Weight");
const wchar_t *disclaimer = json.stringOf(L"TrackResponse.Disclaimer");
int i = 0;
int activityCount = json.SizeOfArray(L"TrackResponse.Shipment.Package.Activity");
wprintf(L"activityCount: %d\n",activityCount);
while (i < activityCount) {
wprintf(L"-- activity %d\n",i);
json.put_I(i);
if (json.HasMember(L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") == true) {
const wchar_t *city = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City");
wprintf(L"city: %s\n",city);
const wchar_t *provinceCode = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode");
const wchar_t *postalCode = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode");
const wchar_t *countryCode = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode");
}
const wchar_t *locationCode = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code");
const wchar_t *locationDescription = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description");
const wchar_t *locationSignedForByName = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName");
const wchar_t *activityStatusType = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].Status.Type");
wprintf(L"activityStatusType: %s\n",activityStatusType);
const wchar_t *activityStatusDescription = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].Status.Description");
wprintf(L"activityStatusDescription: %s\n",activityStatusDescription);
const wchar_t *activityStatusCode = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].Status.Code");
wprintf(L"activityStatusCode: %s\n",activityStatusCode);
const wchar_t *activityDate = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].Date");
const wchar_t *activityTime = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].Time");
if (json.HasMember(L"TrackResponse.Shipment.Package.Activity[i].Document") == true) {
int typeCode = json.IntOf(L"TrackResponse.Shipment.Package.Activity[i].Document.Type.Code");
const wchar_t *typeDescription = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].Document.Type.Description");
const wchar_t *documentContent = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].Document.Content");
const wchar_t *documentFormatCode = json.stringOf(L"TrackResponse.Shipment.Package.Activity[i].Document.Format.Code");
// Format description would be something like "GIF" for a signature image.
const wchar_t *documentFormatDescription = json.stringOf(L"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.
CkStringBuilderW sbImagePath;
sbImagePath.Append(L"qa_output/sig_");
sbImagePath.Append(trackingNumber);
sbImagePath.Append(L".");
sbImagePath.Append(documentFormatDescription);
CkBinDataW imageData;
success = imageData.AppendEncoded(documentContent,L"base64");
// Write to "qa_output/sig_1Z12345E0205271688.GIF"
success = imageData.WriteFile(sbImagePath.getAsString());
}
}
i = i + 1;
}
i = 0;
int refnumCount = json.SizeOfArray(L"TrackResponse.Shipment.Package.ReferenceNumber");
while (i < refnumCount) {
json.put_I(i);
const wchar_t *refnumCode = json.stringOf(L"TrackResponse.Shipment.Package.ReferenceNumber[i].Code");
const wchar_t *refnumValue = json.stringOf(L"TrackResponse.Shipment.Package.ReferenceNumber[i].Value");
i = i + 1;
}
wprintf(L"Success.\n");
}