Lua
Lua
Shippo Validate Existing Address
See more Shippo Examples
Demonstrates how to validate an existing address.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Implements the following CURL command:
-- curl https://api.goshippo.com/addresses/e9dabe9669334b84baad3a08646b2202/validate/ \
-- -H "Authorization: ShippoToken <API_TOKEN>"
http:set_request_header("Authorization", "ShippoToken <API_TOKEN>")
local sbResponseBody = chilkat.StringBuilder.new()
local success = http:quick_get_sb("https://api.goshippo.com/addresses/e9dabe9669334b84baad3a08646b2202/validate/", sbResponseBody)
if not success then
print(http.last_error_text)
return
end
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = http.last_status
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(http.last_header)
print("Failed.")
return
end
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "object_created": "2015-03-30T23:47:11.574Z",
-- "object_updated": "2015-03-30T23:47:11.596Z",
-- "object_id": "67183b2e81e9421f894bfbcdc4236b16",
-- "is_complete": false,
-- "validation_results": {
-- "is_valid": false,
-- "messages": [
-- {
-- "source": "USPS",
-- "code": "Address Not Found",
-- "type": "address_error",
-- "text": "The address as submitted could not be found. Please check for excessive abbreviations in the street address line or in the City name."
-- }
-- ]
-- },
-- "object_owner": "shippotle@goshippo.com",
-- "name": "Shawn Ippotle",
-- "company": "Shippo",
-- "street_no": "",
-- "street1": "215 HIPPO ST.",
-- "street2": "",
-- "city": "SAN FRANCISCO",
-- "state": "CA",
-- "zip": "94107",
-- "country": "US",
-- "phone": ""
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local source = ""
local code = ""
local v_type = ""
local text = ""
local object_created = jResp:string_of("object_created")
local object_updated = jResp:string_of("object_updated")
local object_id = jResp:string_of("object_id")
local is_complete = jResp:bool_of("is_complete")
local validation_resultsIs_valid = jResp:bool_of("validation_results.is_valid")
local object_owner = jResp:string_of("object_owner")
local name = jResp:string_of("name")
local company = jResp:string_of("company")
local street_no = jResp:string_of("street_no")
local street1 = jResp:string_of("street1")
local street2 = jResp:string_of("street2")
local city = jResp:string_of("city")
local state = jResp:string_of("state")
local zip = jResp:string_of("zip")
local country = jResp:string_of("country")
local phone = jResp:string_of("phone")
local i = 0
local count_i = jResp:size_of_array("validation_results.messages")
while i < count_i do
jResp.i = i
source = jResp:string_of("validation_results.messages[i].source")
code = jResp:string_of("validation_results.messages[i].code")
v_type = jResp:string_of("validation_results.messages[i].type")
text = jResp:string_of("validation_results.messages[i].text")
i = i + 1
end