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
(AutoIt) 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.
; This example requires the Chilkat API to have been previously unlocked. ; See Global Unlock Sample for sample code. Local $bSuccess $oHttp = ObjCreate("Chilkat.Http") ; This is the testing endpoint for the tracking API: Local $sUrl = "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. $oJson = ObjCreate("Chilkat.JsonObject") $oJson.UpdateString("UPSSecurity.UsernameToken.Username","UPS_USERNAME") $oJson.UpdateString("UPSSecurity.UsernameToken.Password","UPS_PASSWORD") $oJson.UpdateString("UPSSecurity.ServiceAccessToken.AccessLicenseNumber","UPS_ACCESS_KEY") ; Request all activity... $oJson.UpdateString("TrackRequest.Request.RequestOption","activity") $oJson.UpdateString("TrackRequest.Request.TransactionReference.CustomerContext","Your Test Case Summary Description") $oJson.UpdateString("TrackRequest.InquiryNumber","1Z12345E0205271688") $oSb = ObjCreate("Chilkat.StringBuilder") Local $oResp = $oHttp.PostJson3($sUrl,"application/json",$oJson) If ($oHttp.LastMethodSuccess <> True) Then ConsoleWrite($oHttp.LastErrorText & @CRLF) Exit EndIf ConsoleWrite("status = " & $oResp.StatusCode & @CRLF) ; A 200 response status indicate success. If ($oResp.StatusCode <> 200) Then ConsoleWrite($oResp.BodyStr & @CRLF) ConsoleWrite("Failed." & @CRLF) Exit EndIf $oJson.Load($oResp.BodyStr) $oJson.EmitCompact = False ConsoleWrite($oJson.Emit() & @CRLF) ; { ; "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. Local $statusCode = $oJson.StringOf("TrackResponse.Response.ResponseStatus.Code") Local $statusDescription = $oJson.StringOf("TrackResponse.Response.ResponseStatus.Description") ConsoleWrite("statusCode: " & $statusCode & @CRLF) ConsoleWrite("statusDescription" & $statusDescription & @CRLF) Local $sCustomerContext = $oJson.StringOf("TrackResponse.Response.TransactionReference.CustomerContext") Local $sInquiryNumberCode = $oJson.StringOf("TrackResponse.Shipment.InquiryNumber.Code") Local $sInquiryNumberDescription = $oJson.StringOf("TrackResponse.Shipment.InquiryNumber.Description") Local $sInquiryNumberValue = $oJson.StringOf("TrackResponse.Shipment.InquiryNumber.Value") Local $shipmentTypeCode = $oJson.StringOf("TrackResponse.Shipment.ShipmentType.Code") Local $shipmentTypeDescription = $oJson.StringOf("TrackResponse.Shipment.ShipmentType.Description") Local $shipperNumber = $oJson.StringOf("TrackResponse.Shipment.ShipperNumber") Local $serviceCode = $oJson.StringOf("TrackResponse.Shipment.Service.Code") Local $serviceDescription = $oJson.StringOf("TrackResponse.Shipment.Service.Description") Local $sReferenceNumberCode = $oJson.StringOf("TrackResponse.Shipment.ReferenceNumber.Code") Local $sReferenceNumberValue = $oJson.StringOf("TrackResponse.Shipment.ReferenceNumber.Value") Local $sPickupDate = $oJson.StringOf("TrackResponse.Shipment.PickupDate") Local $sTrackingNumber = $oJson.StringOf("TrackResponse.Shipment.Package.TrackingNumber") Local $sUnitOfMeasurementCode = $oJson.StringOf("TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code") Local $sWeight = $oJson.StringOf("TrackResponse.Shipment.Package.PackageWeight.Weight") Local $sDisclaimer = $oJson.StringOf("TrackResponse.Disclaimer") Local $i = 0 Local $iActivityCount = $oJson.SizeOfArray("TrackResponse.Shipment.Package.Activity") ConsoleWrite("activityCount: " & $iActivityCount & @CRLF) While $i < $iActivityCount ConsoleWrite("-- activity " & $i & @CRLF) $oJson.I = $i If ($oJson.HasMember("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") = True) Then Local $sCity = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City") ConsoleWrite("city: " & $sCity & @CRLF) Local $sProvinceCode = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode") Local $sPostalCode = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode") Local $sCountryCode = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode") EndIf Local $sLocationCode = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code") Local $sLocationDescription = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description") Local $sLocationSignedForByName = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName") Local $sActivityStatusType = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Type") ConsoleWrite("activityStatusType: " & $sActivityStatusType & @CRLF) Local $sActivityStatusDescription = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Description") ConsoleWrite("activityStatusDescription: " & $sActivityStatusDescription & @CRLF) Local $sActivityStatusCode = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Status.Code") ConsoleWrite("activityStatusCode: " & $sActivityStatusCode & @CRLF) Local $sActivityDate = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Date") Local $sActivityTime = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Time") If ($oJson.HasMember("TrackResponse.Shipment.Package.Activity[i].Document") = True) Then Local $iTypeCode = $oJson.IntOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Code") Local $sTypeDescription = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Type.Description") Local $sDocumentContent = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Content") Local $sDocumentFormatCode = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Code") ; Format description would be something like "GIF" for a signature image. Local $sDocumentFormatDescription = $oJson.StringOf("TrackResponse.Shipment.Package.Activity[i].Document.Format.Description") ; 01 - Signature Image ; 02 - Delivery Receipt ; 03 - Free Astray ; 04 - POD If ($iTypeCode = 1) Then ; We have a signature image. Get the image data and save to a file. $oSbImagePath = ObjCreate("Chilkat.StringBuilder") $oSbImagePath.Append("qa_output/sig_") $oSbImagePath.Append($sTrackingNumber) $oSbImagePath.Append(".") $oSbImagePath.Append($sDocumentFormatDescription) $oImageData = ObjCreate("Chilkat.BinData") $bSuccess = $oImageData.AppendEncoded($sDocumentContent,"base64") ; Write to "qa_output/sig_1Z12345E0205271688.GIF" $bSuccess = $oImageData.WriteFile($oSbImagePath.GetAsString()) EndIf EndIf $i = $i + 1 Wend $i = 0 Local $iRefnumCount = $oJson.SizeOfArray("TrackResponse.Shipment.Package.ReferenceNumber") While $i < $iRefnumCount $oJson.I = $i Local $sRefnumCode = $oJson.StringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Code") Local $sRefnumValue = $oJson.StringOf("TrackResponse.Shipment.Package.ReferenceNumber[i].Value") $i = $i + 1 Wend ConsoleWrite("Success." & @CRLF) |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.