Lua
Lua
UPS Address Validation (City, State, Zip)
See more HTTP Misc Examples
Demonstrates making a call to the UPS address validation REST API.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- This is the testing endpoint for address validation:
local 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.
local json = chilkat.JsonObject.new()
json:update_string("AccessRequest.AccessLicenseNumber", "UPS_ACCESS_KEY")
json:update_string("AccessRequest.UserId", "UPS_USERNAME")
json:update_string("AccessRequest.Password", "UPS_PASSWORD")
json:update_string("AddressValidationRequest.Request.TransactionReference.CustomerContext", "Your Customer Context")
json:update_string("AddressValidationRequest.Request.RequestAction", "AV")
json:update_string("AddressValidationRequest.Address.City", "ALPHARETTA")
-- We're making an intentional mistake here by passing CA instead of GA.
json:update_string("AddressValidationRequest.Address.StateProvinceCode", "CA")
json:update_string("AddressValidationRequest.Address.PostalCode", "30005")
local sb = chilkat.StringBuilder.new()
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", url, json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
print("status = " .. resp.status_code)
-- A 200 response status indicate success.
if resp.status_code ~= 200 then
print(resp.body_str)
print("Failed.")
return
end
json:load(resp.body_str)
json.emit_compact = false
print(json:emit())
-- 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.
local customerContext = ""
local statusCode = ""
local statusDescription = ""
local resultRank = ""
local resultQuality = ""
local city = ""
local provinceCode = ""
local postalCodeLowEnd = ""
local postalCodeHighEnd = ""
local rank = ""
local quality = ""
local addressCity = ""
local addressStateProvinceCode = ""
local numResults = json:size_of_array("AddressValidationResponse.AddressValidationResult")
if numResults < 0 then
-- Here's parse code for the above JSON exact response:
customerContext = json:string_of("AddressValidationResponse.Response.TransactionReference.CustomerContext")
statusCode = json:string_of("AddressValidationResponse.Response.ResponseStatusCode")
statusDescription = json:string_of("AddressValidationResponse.Response.ResponseStatusDescription")
resultRank = json:string_of("AddressValidationResponse.AddressValidationResult.Rank")
resultQuality = json:string_of("AddressValidationResponse.AddressValidationResult.Quality")
city = json:string_of("AddressValidationResponse.AddressValidationResult.Address.City")
provinceCode = json:string_of("AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode")
postalCodeLowEnd = json:string_of("AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd")
postalCodeHighEnd = json:string_of("AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd")
print("Exact match!")
print("postal code: " .. postalCodeLowEnd)
else
print("Non-Exact match.")
customerContext = json:string_of("AddressValidationResponse.Response.TransactionReference.CustomerContext")
statusCode = json:string_of("AddressValidationResponse.Response.ResponseStatusCode")
statusDescription = json:string_of("AddressValidationResponse.Response.ResponseStatusDescription")
local i = 0
while i < numResults do
json.i = i
rank = json:string_of("AddressValidationResponse.AddressValidationResult[i].Rank")
print("rank: " .. rank)
quality = json:string_of("AddressValidationResponse.AddressValidationResult[i].Quality")
addressCity = json:string_of("AddressValidationResponse.AddressValidationResult[i].Address.City")
print("addressCity: " .. addressCity)
addressStateProvinceCode = json:string_of("AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode")
postalCodeLowEnd = json:string_of("AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd")
print("postal code: " .. postalCodeLowEnd)
postalCodeHighEnd = json:string_of("AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd")
i = i + 1
end
end