Tcl
Tcl
UPS Address Validation (City, State, Zip)
See more HTTP Misc Examples
Demonstrates making a call to the UPS address validation REST API.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
# 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]
set resp [new_CkHttpResponse]
set success [CkHttp_HttpJson $http "POST" $url $json "application/json" $resp]
if {$success == 0} then {
puts [CkHttp_lastErrorText $http]
delete_CkHttp $http
delete_CkJsonObject $json
delete_CkStringBuilder $sb
delete_CkHttpResponse $resp
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_CkHttp $http
delete_CkJsonObject $json
delete_CkStringBuilder $sb
delete_CkHttpResponse $resp
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_CkHttp $http
delete_CkJsonObject $json
delete_CkStringBuilder $sb
delete_CkHttpResponse $resp