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 Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
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 address validation: set url "https://wwwcie.ups.com/rest/AV" # Send an HTTP request with the following JSON body: # { # "AccessRequest": { # "AccessLicenseNumber": "Your Access License Number", # "UserId": "Your Username", # "Password": "Your Password" # }, # "AddressValidationRequest": { # "Request": { # "TransactionReference": { # "CustomerContext": "Your Customer Context" # }, # "RequestAction": "AV" # }, # "Address": { # "City": "ALPHARETTA", # "StateProvinceCode": "GA", # "PostalCode": "30005" # } # } # } # Build the above JSON. set json [new_CkJsonObject] CkJsonObject_UpdateString $json "AccessRequest.AccessLicenseNumber" "UPS_ACCESS_KEY" CkJsonObject_UpdateString $json "AccessRequest.UserId" "UPS_USERNAME" CkJsonObject_UpdateString $json "AccessRequest.Password" "UPS_PASSWORD" CkJsonObject_UpdateString $json "AddressValidationRequest.Request.TransactionReference.CustomerContext" "Your Customer Context" CkJsonObject_UpdateString $json "AddressValidationRequest.Request.RequestAction" "AV" CkJsonObject_UpdateString $json "AddressValidationRequest.Address.City" "ALPHARETTA" # We're making an intentional mistake here by passing CA instead of GA. CkJsonObject_UpdateString $json "AddressValidationRequest.Address.StateProvinceCode" "CA" CkJsonObject_UpdateString $json "AddressValidationRequest.Address.PostalCode" "30005" 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] # A successful exact response looks like this: # { # "AddressValidationResponse": { # "Response": { # "TransactionReference": { # "CustomerContext": "Your Customer Context" # }, # "ResponseStatusCode": "1", # "ResponseStatusDescription": "Success" # }, # "AddressValidationResult": { # "Rank": "1", # "Quality": "1.0", # "Address": { # "City": "ALPHARETTA", # "StateProvinceCode": "GA" # }, # "PostalCodeLowEnd": "30005", # "PostalCodeHighEnd": "30005" # } # } # } # # A successful response that was not an exact match provides an array of closest matches, like this: # { # "AddressValidationResponse": { # "Response": { # "TransactionReference": { # "CustomerContext": "Your Customer Context" # "Quality": "0.9875", # "Address": { # }, # "ResponseStatusCode": "1", # "ResponseStatusDescription": "Success" # }, # "AddressValidationResult": [ # { # "Rank": "1", # "City": "ALPHARETTA", # "StateProvinceCode": "GA" # }, # "PostalCodeLowEnd": "30005", # "PostalCodeHighEnd": "30005" # }, # { # "Rank": "2", # "Quality": "0.9750", # "Address": { # "City": "ALPHARETTA", # "StateProvinceCode": "GA" # }, # "PostalCodeLowEnd": "30004", # "PostalCodeHighEnd": "30004" # }, # { # "Rank": "3", # "Quality": "0.9750", # "Address": { # "City": "ALPHARETTA", # "StateProvinceCode": "GA" # }, # "PostalCodeLowEnd": "30009", # "PostalCodeHighEnd": "30009" # } # ] # } # } # Use the online tool at Generate JSON Parsing Code # to generate JSON parsing code. set numResults [CkJsonObject_SizeOfArray $json "AddressValidationResponse.AddressValidationResult"] if {$numResults < 0} then { # Here's parse code for the above JSON exact response: set customerContext [CkJsonObject_stringOf $json "AddressValidationResponse.Response.TransactionReference.CustomerContext"] set statusCode [CkJsonObject_stringOf $json "AddressValidationResponse.Response.ResponseStatusCode"] set statusDescription [CkJsonObject_stringOf $json "AddressValidationResponse.Response.ResponseStatusDescription"] set resultRank [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult.Rank"] set resultQuality [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult.Quality"] set city [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult.Address.City"] set provinceCode [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode"] set postalCodeLowEnd [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd"] set postalCodeHighEnd [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd"] puts "Exact match!" puts "postal code: $postalCodeLowEnd" } else { puts "Non-Exact match." set customerContext [CkJsonObject_stringOf $json "AddressValidationResponse.Response.TransactionReference.CustomerContext"] set statusCode [CkJsonObject_stringOf $json "AddressValidationResponse.Response.ResponseStatusCode"] set statusDescription [CkJsonObject_stringOf $json "AddressValidationResponse.Response.ResponseStatusDescription"] set i 0 while {$i < $numResults} { CkJsonObject_put_I $json $i set rank [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult[i].Rank"] puts "rank: $rank" set quality [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult[i].Quality"] set addressCity [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult[i].Address.City"] puts "addressCity: $addressCity" set addressStateProvinceCode [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode"] set postalCodeLowEnd [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd"] puts "postal code: $postalCodeLowEnd" set postalCodeHighEnd [CkJsonObject_stringOf $json "AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd"] set i [expr $i + 1] } } delete_CkHttpResponse $resp delete_CkHttp $http delete_CkJsonObject $json delete_CkStringBuilder $sb |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.