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
(Tcl) 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.
load ./chilkat.dll # This example requires the Chilkat API to have been previously unlocked. # See Global Unlock Sample for sample code. set http [new_CkHttp] # This is the testing endpoint for the tracking API: set 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. set json [new_CkJsonObject] 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" set sb [new_CkStringBuilder] # resp is a CkHttpResponse set resp [CkHttp_PostJson3 $http $url "application/json" $json] if {[CkHttp_get_LastMethodSuccess $http] != 1} then { puts [CkHttp_lastErrorText $http] delete_CkHttp $http delete_CkJsonObject $json delete_CkStringBuilder $sb exit } puts "status = [CkHttpResponse_get_StatusCode $resp]" # A 200 response status indicate success. if {[CkHttpResponse_get_StatusCode $resp] != 200} then { puts [CkHttpResponse_bodyStr $resp] puts "Failed." delete_CkHttpResponse $resp delete_CkHttp $http delete_CkJsonObject $json delete_CkStringBuilder $sb exit } CkJsonObject_Load $json [CkHttpResponse_bodyStr $resp] CkJsonObject_put_EmitCompact $json 0 puts [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. set statusCode [CkJsonObject_stringOf $json "TrackResponse.Response.ResponseStatus.Code"] set statusDescription [CkJsonObject_stringOf $json "TrackResponse.Response.ResponseStatus.Description"] puts "statusCode: $statusCode" puts "statusDescription$statusDescription" set customerContext [CkJsonObject_stringOf $json "TrackResponse.Response.TransactionReference.CustomerContext"] set inquiryNumberCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.InquiryNumber.Code"] set inquiryNumberDescription [CkJsonObject_stringOf $json "TrackResponse.Shipment.InquiryNumber.Description"] set inquiryNumberValue [CkJsonObject_stringOf $json "TrackResponse.Shipment.InquiryNumber.Value"] set shipmentTypeCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.ShipmentType.Code"] set shipmentTypeDescription [CkJsonObject_stringOf $json "TrackResponse.Shipment.ShipmentType.Description"] set shipperNumber [CkJsonObject_stringOf $json "TrackResponse.Shipment.ShipperNumber"] set serviceCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Service.Code"] set serviceDescription [CkJsonObject_stringOf $json "TrackResponse.Shipment.Service.Description"] set referenceNumberCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.ReferenceNumber.Code"] set referenceNumberValue [CkJsonObject_stringOf $json "TrackResponse.Shipment.ReferenceNumber.Value"] set pickupDate [CkJsonObject_stringOf $json "TrackResponse.Shipment.PickupDate"] set trackingNumber [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.TrackingNumber"] set unitOfMeasurementCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.PackageWeight.UnitOfMeasurement.Code"] set weight [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.PackageWeight.Weight"] set disclaimer [CkJsonObject_stringOf $json "TrackResponse.Disclaimer"] set i 0 set activityCount [CkJsonObject_SizeOfArray $json "TrackResponse.Shipment.Package.Activity"] puts "activityCount: $activityCount" while {$i < $activityCount} { puts "-- activity $i" CkJsonObject_put_I $json $i if {[CkJsonObject_HasMember $json "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City"] == 1} then { set city [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.City"] puts "city: $city" set provinceCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.StateProvinceCode"] set postalCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.PostalCode"] set countryCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Address.CountryCode"] } set locationCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Code"] set locationDescription [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.Description"] set locationSignedForByName [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].ActivityLocation.SignedForByName"] set activityStatusType [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].Status.Type"] puts "activityStatusType: $activityStatusType" set activityStatusDescription [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].Status.Description"] puts "activityStatusDescription: $activityStatusDescription" set activityStatusCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].Status.Code"] puts "activityStatusCode: $activityStatusCode" set activityDate [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].Date"] set activityTime [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].Time"] if {[CkJsonObject_HasMember $json "TrackResponse.Shipment.Package.Activity[i].Document"] == 1} then { set typeCode [CkJsonObject_IntOf $json "TrackResponse.Shipment.Package.Activity[i].Document.Type.Code"] set typeDescription [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].Document.Type.Description"] set documentContent [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].Document.Content"] set documentFormatCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.Activity[i].Document.Format.Code"] # Format description would be something like "GIF" for a signature image. set 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 { # We have a signature image. Get the image data and save to a file. set sbImagePath [new_CkStringBuilder] CkStringBuilder_Append $sbImagePath "qa_output/sig_" CkStringBuilder_Append $sbImagePath $trackingNumber CkStringBuilder_Append $sbImagePath "." CkStringBuilder_Append $sbImagePath $documentFormatDescription set imageData [new_CkBinData] set success [CkBinData_AppendEncoded $imageData $documentContent "base64"] # Write to "qa_output/sig_1Z12345E0205271688.GIF" set success [CkBinData_WriteFile $imageData [CkStringBuilder_getAsString $sbImagePath]] } } set i [expr $i + 1] } set i 0 set refnumCount [CkJsonObject_SizeOfArray $json "TrackResponse.Shipment.Package.ReferenceNumber"] while {$i < $refnumCount} { CkJsonObject_put_I $json $i set refnumCode [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.ReferenceNumber[i].Code"] set refnumValue [CkJsonObject_stringOf $json "TrackResponse.Shipment.Package.ReferenceNumber[i].Value"] set i [expr $i + 1] } puts "Success." delete_CkHttpResponse $resp delete_CkHttp $http delete_CkJsonObject $json delete_CkStringBuilder $sb delete_CkStringBuilder $sbImagePath delete_CkBinData $imageData |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.