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
(Unicode 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_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkStringBuilderW.h> #include <C_CkHttpResponseW.h> #include <C_CkBinDataW.h> void ChilkatSample(void) { BOOL success; HCkHttpW http; const wchar_t *url; HCkJsonObjectW json; HCkStringBuilderW sb; HCkHttpResponseW resp; const wchar_t *statusCode; const wchar_t *statusDescription; const wchar_t *customerContext; const wchar_t *inquiryNumberCode; const wchar_t *inquiryNumberDescription; const wchar_t *inquiryNumberValue; const wchar_t *shipmentTypeCode; const wchar_t *shipmentTypeDescription; const wchar_t *shipperNumber; const wchar_t *serviceCode; const wchar_t *serviceDescription; const wchar_t *referenceNumberCode; const wchar_t *referenceNumberValue; const wchar_t *pickupDate; const wchar_t *trackingNumber; const wchar_t *unitOfMeasurementCode; const wchar_t *weight; const wchar_t *disclaimer; int i; int activityCount; const wchar_t *city; const wchar_t *provinceCode; const wchar_t *postalCode; const wchar_t *countryCode; const wchar_t *locationCode; const wchar_t *locationDescription; const wchar_t *locationSignedForByName; const wchar_t *activityStatusType; const wchar_t *activityStatusDescription; const wchar_t *activityStatusCode; const wchar_t *activityDate; const wchar_t *activityTime; int typeCode; const wchar_t *typeDescription; const wchar_t *documentContent; const wchar_t *documentFormatCode; const wchar_t *documentFormatDescription; HCkStringBuilderW sbImagePath; HCkBinDataW imageData; int refnumCount; const wchar_t *refnumCode; const wchar_t *refnumValue; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); // This is the testing endpoint for the tracking API: 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. json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"UPSSecurity.UsernameToken.Username",L"UPS_USERNAME"); CkJsonObjectW_UpdateString(json,L"UPSSecurity.UsernameToken.Password",L"UPS_PASSWORD"); CkJsonObjectW_UpdateString(json,L"UPSSecurity.ServiceAccessToken.AccessLicenseNumber",L"UPS_ACCESS_KEY"); // Request all activity... CkJsonObjectW_UpdateString(json,L"TrackRequest.Request.RequestOption",L"activity"); CkJsonObjectW_UpdateString(json,L"TrackRequest.Request.TransactionReference.CustomerContext",L"Your Test Case Summary Description"); CkJsonObjectW_UpdateString(json,L"TrackRequest.InquiryNumber",L"1Z12345E0205271688"); sb = CkStringBuilderW_Create(); resp = CkHttpW_PostJson3(http,url,L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); return; } wprintf(L"status = %d\n",CkHttpResponseW_getStatusCode(resp)); // A 200 response status indicate success. if (CkHttpResponseW_getStatusCode(resp) != 200) { wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); return; } CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp)); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_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 = CkJsonObjectW_stringOf(json,L"TrackResponse.Response.ResponseStatus.Code"); statusDescription = CkJsonObjectW_stringOf(json,L"TrackResponse.Response.ResponseStatus.Description"); wprintf(L"statusCode: %s\n",statusCode); wprintf(L"statusDescription%s\n",statusDescription); customerContext = CkJsonObjectW_stringOf(json,L"TrackResponse.Response.TransactionReference.CustomerContext"); inquiryNumberCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.InquiryNumber.Code"); inquiryNumberDescription = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.InquiryNumber.Description"); inquiryNumberValue = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.InquiryNumber.Value"); shipmentTypeCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.ShipmentType.Code"); shipmentTypeDescription = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.ShipmentType.Description"); shipperNumber = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.ShipperNumber"); serviceCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Service.Code"); serviceDescription = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Service.Description"); referenceNumberCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.ReferenceNumber.Code"); referenceNumberValue = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.ReferenceNumber.Value"); pickupDate = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.PickupDate"); trackingNumber = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.TrackingNumber"); unitOfMeasurementCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code"); weight = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.PackageWeight.Weight"); disclaimer = CkJsonObjectW_stringOf(json,L"TrackResponse.Disclaimer"); i = 0; activityCount = CkJsonObjectW_SizeOfArray(json,L"TrackResponse.Shipment.Package.Activity"); wprintf(L"activityCount: %d\n",activityCount); while (i < activityCount) { wprintf(L"-- activity %d\n",i); CkJsonObjectW_putI(json,i); if (CkJsonObjectW_HasMember(json,L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") == TRUE) { city = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City"); wprintf(L"city: %s\n",city); provinceCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode"); postalCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode"); countryCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode"); } locationCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code"); locationDescription = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description"); locationSignedForByName = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName"); activityStatusType = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].Status.Type"); wprintf(L"activityStatusType: %s\n",activityStatusType); activityStatusDescription = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].Status.Description"); wprintf(L"activityStatusDescription: %s\n",activityStatusDescription); activityStatusCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].Status.Code"); wprintf(L"activityStatusCode: %s\n",activityStatusCode); activityDate = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].Date"); activityTime = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].Time"); if (CkJsonObjectW_HasMember(json,L"TrackResponse.Shipment.Package.Activity[i].Document") == TRUE) { typeCode = CkJsonObjectW_IntOf(json,L"TrackResponse.Shipment.Package.Activity[i].Document.Type.Code"); typeDescription = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].Document.Type.Description"); documentContent = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].Document.Content"); documentFormatCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.Activity[i].Document.Format.Code"); // Format description would be something like "GIF" for a signature image. documentFormatDescription = CkJsonObjectW_stringOf(json,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. sbImagePath = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbImagePath,L"qa_output/sig_"); CkStringBuilderW_Append(sbImagePath,trackingNumber); CkStringBuilderW_Append(sbImagePath,L"."); CkStringBuilderW_Append(sbImagePath,documentFormatDescription); imageData = CkBinDataW_Create(); success = CkBinDataW_AppendEncoded(imageData,documentContent,L"base64"); // Write to "qa_output/sig_1Z12345E0205271688.GIF" success = CkBinDataW_WriteFile(imageData,CkStringBuilderW_getAsString(sbImagePath)); } } i = i + 1; } i = 0; refnumCount = CkJsonObjectW_SizeOfArray(json,L"TrackResponse.Shipment.Package.ReferenceNumber"); while (i < refnumCount) { CkJsonObjectW_putI(json,i); refnumCode = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.ReferenceNumber[i].Code"); refnumValue = CkJsonObjectW_stringOf(json,L"TrackResponse.Shipment.Package.ReferenceNumber[i].Value"); i = i + 1; } wprintf(L"Success.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); CkStringBuilderW_Dispose(sbImagePath); CkBinDataW_Dispose(imageData); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.