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
(Delphi DLL) 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.
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, BinData, HttpResponse, JsonObject; ... procedure TForm1.Button1Click(Sender: TObject); var success: Boolean; http: HCkHttp; url: PWideChar; json: HCkJsonObject; sb: HCkStringBuilder; resp: HCkHttpResponse; statusCode: PWideChar; statusDescription: PWideChar; customerContext: PWideChar; inquiryNumberCode: PWideChar; inquiryNumberDescription: PWideChar; inquiryNumberValue: PWideChar; shipmentTypeCode: PWideChar; shipmentTypeDescription: PWideChar; shipperNumber: PWideChar; serviceCode: PWideChar; serviceDescription: PWideChar; referenceNumberCode: PWideChar; referenceNumberValue: PWideChar; pickupDate: PWideChar; trackingNumber: PWideChar; unitOfMeasurementCode: PWideChar; weight: PWideChar; disclaimer: PWideChar; i: Integer; activityCount: Integer; city: PWideChar; provinceCode: PWideChar; postalCode: PWideChar; countryCode: PWideChar; locationCode: PWideChar; locationDescription: PWideChar; locationSignedForByName: PWideChar; activityStatusType: PWideChar; activityStatusDescription: PWideChar; activityStatusCode: PWideChar; activityDate: PWideChar; activityTime: PWideChar; typeCode: Integer; typeDescription: PWideChar; documentContent: PWideChar; documentFormatCode: PWideChar; documentFormatDescription: PWideChar; sbImagePath: HCkStringBuilder; imageData: HCkBinData; refnumCount: Integer; refnumCode: PWideChar; refnumValue: PWideChar; begin // 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) then begin Memo1.Lines.Add(CkHttp__lastErrorText(http)); Exit; end; Memo1.Lines.Add('status = ' + IntToStr(CkHttpResponse_getStatusCode(resp))); // A 200 response status indicate success. if (CkHttpResponse_getStatusCode(resp) <> 200) then begin Memo1.Lines.Add(CkHttpResponse__bodyStr(resp)); Memo1.Lines.Add('Failed.'); CkHttpResponse_Dispose(resp); Exit; end; CkJsonObject_Load(json,CkHttpResponse__bodyStr(resp)); CkJsonObject_putEmitCompact(json,False); Memo1.Lines.Add(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'); Memo1.Lines.Add('statusCode: ' + statusCode); Memo1.Lines.Add('statusDescription' + 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'); Memo1.Lines.Add('activityCount: ' + IntToStr(activityCount)); while i < activityCount do begin Memo1.Lines.Add('-- activity ' + IntToStr(i)); CkJsonObject_putI(json,i); if (CkJsonObject_HasMember(json,'TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City') = True) then begin city := CkJsonObject__stringOf(json,'TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City'); Memo1.Lines.Add('city: ' + 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'); end; 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'); Memo1.Lines.Add('activityStatusType: ' + activityStatusType); activityStatusDescription := CkJsonObject__stringOf(json,'TrackResponse.Shipment.Package.Activity[i].Status.Description'); Memo1.Lines.Add('activityStatusDescription: ' + activityStatusDescription); activityStatusCode := CkJsonObject__stringOf(json,'TrackResponse.Shipment.Package.Activity[i].Status.Code'); Memo1.Lines.Add('activityStatusCode: ' + 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) then begin 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) then begin // 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)); end; end; i := i + 1; end; i := 0; refnumCount := CkJsonObject_SizeOfArray(json,'TrackResponse.Shipment.Package.ReferenceNumber'); while i < refnumCount do begin 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; end; Memo1.Lines.Add('Success.'); CkHttpResponse_Dispose(resp); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sb); CkStringBuilder_Dispose(sbImagePath); CkBinData_Dispose(imageData); end; |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.