Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) UPS Tracking APIDemonstrates making a call to the UPS tracking REST API. Parses the tracking response and extracts the base64 signature image to a gif file.
#include <C_CkHttp.h> #include <C_CkJsonObject.h> #include <C_CkStringBuilder.h> #include <C_CkHttpResponse.h> #include <C_CkBinData.h> void ChilkatSample(void) { BOOL success; HCkHttp http; const char *url; HCkJsonObject json; HCkStringBuilder sb; HCkHttpResponse resp; const char *statusCode; const char *statusDescription; const char *customerContext; const char *inquiryNumberCode; const char *inquiryNumberDescription; const char *inquiryNumberValue; const char *shipmentTypeCode; const char *shipmentTypeDescription; const char *shipperNumber; const char *serviceCode; const char *serviceDescription; const char *referenceNumberCode; const char *referenceNumberValue; const char *pickupDate; const char *trackingNumber; const char *unitOfMeasurementCode; const char *weight; const char *disclaimer; int i; int activityCount; const char *city; const char *provinceCode; const char *postalCode; const char *countryCode; const char *locationCode; const char *locationDescription; const char *locationSignedForByName; const char *activityStatusType; const char *activityStatusDescription; const char *activityStatusCode; const char *activityDate; const char *activityTime; int typeCode; const char *typeDescription; const char *documentContent; const char *documentFormatCode; const char *documentFormatDescription; HCkStringBuilder sbImagePath; HCkBinData imageData; int refnumCount; const char *refnumCode; const char *refnumValue; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttp_Create(); // This is the testing endpoint for the tracking API: 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. json = CkJsonObject_Create(); CkJsonObject_UpdateString(json,"UPSSecurity.UsernameToken.Username","UPS_USERNAME"); CkJsonObject_UpdateString(json,"UPSSecurity.UsernameToken.Password","UPS_PASSWORD"); CkJsonObject_UpdateString(json,"UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY"); // Request all activity... CkJsonObject_UpdateString(json,"TrackRequest.Request.RequestOption","activity"); CkJsonObject_UpdateString(json,"TrackRequest.Request.TransactionReference.CustomerContext","Your Test Case Summary Description"); CkJsonObject_UpdateString(json,"TrackRequest.InquiryNumber","1Z12345E0205271688"); sb = CkStringBuilder_Create(); resp = CkHttp_PostJson3(http,url,"application/json",json); if (CkHttp_getLastMethodSuccess(http) != TRUE) { printf("%s\n",CkHttp_lastErrorText(http)); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sb); return; } printf("status = %d\n",CkHttpResponse_getStatusCode(resp)); // A 200 response status indicate success. if (CkHttpResponse_getStatusCode(resp) != 200) { printf("%s\n",CkHttpResponse_bodyStr(resp)); printf("Failed.\n"); CkHttpResponse_Dispose(resp); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sb); return; } CkJsonObject_Load(json,CkHttpResponse_bodyStr(resp)); CkJsonObject_putEmitCompact(json,FALSE); printf("%s\n",CkJsonObject_emit(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 = CkJsonObject_stringOf(json,"TrackResponse.Response.ResponseStatus.Code"); statusDescription = CkJsonObject_stringOf(json,"TrackResponse.Response.ResponseStatus.Description"); printf("statusCode: %s\n",statusCode); printf("statusDescription%s\n",statusDescription); customerContext = CkJsonObject_stringOf(json,"TrackResponse.Response.TransactionReference.CustomerContext"); inquiryNumberCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.InquiryNumber.Code"); inquiryNumberDescription = CkJsonObject_stringOf(json,"TrackResponse.Shipment.InquiryNumber.Description"); inquiryNumberValue = CkJsonObject_stringOf(json,"TrackResponse.Shipment.InquiryNumber.Value"); shipmentTypeCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.ShipmentType.Code"); shipmentTypeDescription = CkJsonObject_stringOf(json,"TrackResponse.Shipment.ShipmentType.Description"); shipperNumber = CkJsonObject_stringOf(json,"TrackResponse.Shipment.ShipperNumber"); serviceCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Service.Code"); serviceDescription = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Service.Description"); referenceNumberCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.ReferenceNumber.Code"); referenceNumberValue = CkJsonObject_stringOf(json,"TrackResponse.Shipment.ReferenceNumber.Value"); pickupDate = CkJsonObject_stringOf(json,"TrackResponse.Shipment.PickupDate"); trackingNumber = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.TrackingNumber"); unitOfMeasurementCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code"); weight = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.PackageWeight.Weight"); disclaimer = CkJsonObject_stringOf(json,"TrackResponse.Disclaimer"); i = 0; activityCount = CkJsonObject_SizeOfArray(json,"TrackResponse.Shipment.Package.Activity"); printf("activityCount: %d\n",activityCount); while (i < activityCount) { printf("-- activity %d\n",i); CkJsonObject_putI(json,i); if (CkJsonObject_HasMember(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") == TRUE) { city = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City"); printf("city: %s\n",city); provinceCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode"); postalCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode"); countryCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode"); } locationCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code"); locationDescription = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description"); locationSignedForByName = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName"); activityStatusType = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].Status.Type"); printf("activityStatusType: %s\n",activityStatusType); activityStatusDescription = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].Status.Description"); printf("activityStatusDescription: %s\n",activityStatusDescription); activityStatusCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].Status.Code"); printf("activityStatusCode: %s\n",activityStatusCode); activityDate = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].Date"); activityTime = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].Time"); if (CkJsonObject_HasMember(json,"TrackResponse.Shipment.Package.Activity[i].Document") == TRUE) { typeCode = CkJsonObject_IntOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Type.Code"); typeDescription = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Type.Description"); documentContent = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Content"); documentFormatCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.Activity[i].Document.Format.Code"); // Format description would be something like "GIF" for a signature image. documentFormatDescription = CkJsonObject_stringOf(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 = CkStringBuilder_Create(); CkStringBuilder_Append(sbImagePath,"qa_output/sig_"); CkStringBuilder_Append(sbImagePath,trackingNumber); CkStringBuilder_Append(sbImagePath,"."); CkStringBuilder_Append(sbImagePath,documentFormatDescription); imageData = CkBinData_Create(); success = CkBinData_AppendEncoded(imageData,documentContent,"base64"); // Write to "qa_output/sig_1Z12345E0205271688.GIF" success = CkBinData_WriteFile(imageData,CkStringBuilder_getAsString(sbImagePath)); } } i = i + 1; } i = 0; refnumCount = CkJsonObject_SizeOfArray(json,"TrackResponse.Shipment.Package.ReferenceNumber"); while (i < refnumCount) { CkJsonObject_putI(json,i); refnumCode = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.ReferenceNumber[i].Code"); refnumValue = CkJsonObject_stringOf(json,"TrackResponse.Shipment.Package.ReferenceNumber[i].Value"); i = i + 1; } printf("Success.\n"); CkHttpResponse_Dispose(resp); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sb); CkStringBuilder_Dispose(sbImagePath); CkBinData_Dispose(imageData); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.