Lua
Lua
Shippo Validate Global Address
See more Shippo Examples
Demonstrates how to validate a global 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/ \
-- -H "Authorization: ShippoToken <API_TOKEN>" \
-- -d name="Shawn Ippotle" \
-- -d company="Shippo" \
-- -d street1="Kortrijksesteenweg 1005" \
-- -d city="Gent" \
-- -d zip=9000 \
-- -d country="BE" \
-- -d email="shippotle@goshippo.com"\
-- -d validate=true
local req = chilkat.HttpRequest.new()
req.http_verb = "POST"
req.path = "/addresses/"
req.content_type = "application/x-www-form-urlencoded"
req:add_param("name", "Shawn Ippotle")
req:add_param("company", "Shippo")
req:add_param("street1", "Kortrijksesteenweg 1005")
req:add_param("city", "Gent")
req:add_param("zip", "9000")
req:add_param("country", "BE")
req:add_param("email", "shippotle@goshippo.com")
req:add_param("validate", "true")
req:add_header("Authorization", "ShippoToken <API_TOKEN>")
local resp = chilkat.HttpResponse.new()
local success = http:http_req("https://api.goshippo.com/addresses/", req, resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "object_created": "2017-07-26T17:52:37.305Z",
-- "object_updated": "2017-07-26T17:52:37.351Z",
-- "object_id": "b7f9709df3914d1ca6efe4c30e7b0572",
-- "is_complete": true,
-- "validation_results": {
-- "is_valid": true,
-- "messages": [
-- {
-- "source": "Shippo Address Validator",
-- "type": "address_correction",
-- "code": "administrative_area_change",
-- "text": "The administrative area (state or province) was added or changed."
-- },
-- {
-- "source": "Shippo Address Validator",
-- "code": "geocoded_rooftop",
-- "text": "The record was geocoded down to rooftop level, meaning the point is within the property boundaries (most often the center)."
-- },
-- {
-- "source": "Shippo Address Validator",
-- "code": "premises_full",
-- "text": "The address has been verified to the Premise (House or Building) Level, which is the highest level possible with the reference data."
-- }
-- ]
-- },
-- "object_owner": "hippo@goshippo.com",
-- "name": "Hippo Shippo",
-- "company": "Shippo",
-- "street_no": "2",
-- "street1": "Unter den Linden",
-- "street2": "",
-- "street3": "",
-- "city": "Berlin",
-- "state": "",
-- "zip": "10117",
-- "country": "DE",
-- "longitude": "13.39751",
-- "latitude": "52.51785",
-- "phone": "14151234567",
-- "email": "hippo@goshippo.com",
-- "is_residential": null,
-- "metadata": "",
-- "test": false
-- }
-- 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 v_type = ""
local code = ""
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 street3 = jResp:string_of("street3")
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 longitude = jResp:string_of("longitude")
local latitude = jResp:string_of("latitude")
local phone = jResp:string_of("phone")
local email = jResp:string_of("email")
local is_residential = jResp:string_of("is_residential")
local metadata = jResp:string_of("metadata")
local test = jResp:bool_of("test")
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")
v_type = jResp:string_of("validation_results.messages[i].type")
code = jResp:string_of("validation_results.messages[i].code")
text = jResp:string_of("validation_results.messages[i].text")
i = i + 1
end