Chilkat Online Tools

PowerBuilder / ShipEngine Walkthrough / Validate an address (corrected)

Back to Collection Items

integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Jarr
oleobject loo_JsonObj_1
oleobject loo_SbRequestBody
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JarrResp
integer li_RespStatusCode
oleobject loo_Json
string ls_Status
string ls_Name
string ls_Phone
string ls_Company_name
string ls_Address_line1
string ls_Address_line2
string ls_Address_line3
string ls_City_locality
string ls_State_province
string ls_Postal_code
string ls_Country_code
string ls_Address_residential_indicator
string ls_Matched_addressName
string ls_Matched_addressPhone
string ls_Matched_addressCompany_name
string ls_Matched_addressAddress_line1
string ls_Matched_addressAddress_line2
string ls_Matched_addressAddress_line3
string ls_Matched_addressCity_locality
string ls_Matched_addressState_province
string ls_Matched_addressPostal_code
string ls_Matched_addressCountry_code
string ls_Matched_addressAddress_residential_indicator
integer j
integer li_Count_j
integer i
integer li_Count_i

// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Use this online tool to generate code from sample JSON: Generate Code to Create JSON

// The following JSON is sent in the request body.

// [
//   {
//     "address_line1": "525 S Winchester Blvd",
//     "city_locality": "Austin",
//     "state_province": "Texas",
//     "postal_code": "95128",
//     "country_code": "US"
//   }
// ]

loo_Jarr = create oleobject
li_rc = loo_Jarr.ConnectToNewObject("Chilkat_9_5_0.JsonArray")

loo_Jarr.AddObjectAt(-1)
loo_JsonObj_1 = loo_Jarr.ObjectAt(loo_Jarr.Size - 1)
loo_JsonObj_1.UpdateString("address_line1","525 S Winchester Blvd")
loo_JsonObj_1.UpdateString("city_locality","Austin")
loo_JsonObj_1.UpdateString("state_province","Texas")
loo_JsonObj_1.UpdateString("postal_code","95128")
loo_JsonObj_1.UpdateString("country_code","US")
destroy loo_JsonObj_1

loo_Http.SetRequestHeader("API-Key","{{API_KEY}}")
loo_Http.SetRequestHeader("Content-Type","application/json")

loo_SbRequestBody = create oleobject
li_rc = loo_SbRequestBody.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_Jarr.EmitSb(loo_SbRequestBody)

loo_Resp = loo_Http.PTextSb("POST","https://api.shipengine.com/v1/addresses/validate",loo_SbRequestBody,"utf-8","application/json",0,0)
if loo_Http.LastMethodSuccess = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Jarr
    destroy loo_SbRequestBody
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)

loo_JarrResp = create oleobject
li_rc = loo_JarrResp.ConnectToNewObject("Chilkat_9_5_0.JsonArray")

loo_JarrResp.LoadSb(loo_SbResponseBody)
loo_JarrResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JarrResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Resp
    destroy loo_Http
    destroy loo_Jarr
    destroy loo_SbRequestBody
    destroy loo_SbResponseBody
    destroy loo_JarrResp
    return
end if

destroy loo_Resp

// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)

// [
//   {
//     "status": "verified",
//     "original_address": {
//       "name": null,
//       "phone": null,
//       "company_name": null,
//       "address_line1": "525 S Winchester Blvd",
//       "address_line2": null,
//       "address_line3": null,
//       "city_locality": "Austin",
//       "state_province": "Texas",
//       "postal_code": "95128",
//       "country_code": "US",
//       "address_residential_indicator": "unknown"
//     },
//     "matched_address": {
//       "name": null,
//       "phone": null,
//       "company_name": null,
//       "address_line1": "525 S WINCHESTER BLVD",
//       "address_line2": "",
//       "address_line3": null,
//       "city_locality": "SAN JOSE",
//       "state_province": "CA",
//       "postal_code": "95128-2537",
//       "country_code": "US",
//       "address_residential_indicator": "no"
//     },
//     "messages": [
//     ]
//   }
// ]

// Sample code for parsing the JSON response...
// Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

i = 0
li_Count_i = loo_JarrResp.Size
do while i < li_Count_i
    loo_Json = loo_JarrResp.ObjectAt(i)
    ls_Status = loo_Json.StringOf("status")
    ls_Name = loo_Json.StringOf("original_address.name")
    ls_Phone = loo_Json.StringOf("original_address.phone")
    ls_Company_name = loo_Json.StringOf("original_address.company_name")
    ls_Address_line1 = loo_Json.StringOf("original_address.address_line1")
    ls_Address_line2 = loo_Json.StringOf("original_address.address_line2")
    ls_Address_line3 = loo_Json.StringOf("original_address.address_line3")
    ls_City_locality = loo_Json.StringOf("original_address.city_locality")
    ls_State_province = loo_Json.StringOf("original_address.state_province")
    ls_Postal_code = loo_Json.StringOf("original_address.postal_code")
    ls_Country_code = loo_Json.StringOf("original_address.country_code")
    ls_Address_residential_indicator = loo_Json.StringOf("original_address.address_residential_indicator")
    ls_Matched_addressName = loo_Json.StringOf("matched_address.name")
    ls_Matched_addressPhone = loo_Json.StringOf("matched_address.phone")
    ls_Matched_addressCompany_name = loo_Json.StringOf("matched_address.company_name")
    ls_Matched_addressAddress_line1 = loo_Json.StringOf("matched_address.address_line1")
    ls_Matched_addressAddress_line2 = loo_Json.StringOf("matched_address.address_line2")
    ls_Matched_addressAddress_line3 = loo_Json.StringOf("matched_address.address_line3")
    ls_Matched_addressCity_locality = loo_Json.StringOf("matched_address.city_locality")
    ls_Matched_addressState_province = loo_Json.StringOf("matched_address.state_province")
    ls_Matched_addressPostal_code = loo_Json.StringOf("matched_address.postal_code")
    ls_Matched_addressCountry_code = loo_Json.StringOf("matched_address.country_code")
    ls_Matched_addressAddress_residential_indicator = loo_Json.StringOf("matched_address.address_residential_indicator")
    j = 0
    li_Count_j = loo_Json.SizeOfArray("messages")
    do while j < li_Count_j
        loo_Json.J = j
        j = j + 1
    loop
    destroy loo_Json
    i = i + 1
loop


destroy loo_Http
destroy loo_Jarr
destroy loo_SbRequestBody
destroy loo_SbResponseBody
destroy loo_JarrResp

Curl Command

curl -X POST
	-H "API-Key: {{API_KEY}}"
	-H "Content-Type: application/json"
	-d '[
	{
		"address_line1": "525 S Winchester Blvd",
		"city_locality": "Austin",
		"state_province": "Texas",
		"postal_code": "95128",
		"country_code": "US"
	}
]'
https://api.shipengine.com/v1/addresses/validate

Postman Collection Item JSON

{
  "name": "Validate an address (corrected)",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "name": "Content-Type",
        "type": "text",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "[\n\t{\n\t\t\"address_line1\": \"525 S Winchester Blvd\",\n\t\t\"city_locality\": \"Austin\",\n\t\t\"state_province\": \"Texas\",\n\t\t\"postal_code\": \"95128\",\n\t\t\"country_code\": \"US\"\n\t}\n]"
    },
    "url": {
      "raw": "https://api.shipengine.com/v1/addresses/validate",
      "protocol": "https",
      "host": [
        "api",
        "shipengine",
        "com"
      ],
      "path": [
        "v1",
        "addresses",
        "validate"
      ]
    },
    "description": "This request demonstrates ShipEngine's ability to automatically correct some addresses. In this case, the `city_locality` and `state_province` do not match the `postal_code`, and ShipEngine is able to determine that the _correct_ values are San Jose, California\". We're able to determine this because there is no \"525 S Winchester Blvd\" in Austin, Texas, but there _is_ one in San Jose, California, which matches the `postal_code`. However, if the street address had been a valid Austin address (such as 4009 Marathon Blvd), then the `postal_code` would have been corrected instead."
  },
  "response": [
    {
      "name": "Validate an address (corrected)",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n\t{\n\t\t\"address_line1\": \"525 S Winchester Blvd\",\n\t\t\"city_locality\": \"Austin\",\n\t\t\"state_province\": \"Texas\",\n\t\t\"postal_code\": \"95128\",\n\t\t\"country_code\": \"US\"\n\t}\n]",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "https://api.shipengine.com/v1/addresses/validate",
          "protocol": "https",
          "host": [
            "api",
            "shipengine",
            "com"
          ],
          "path": [
            "v1",
            "addresses",
            "validate"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Date",
          "value": "Wed, 18 Sep 2019 14:23:14 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "845"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "cf2d5c37-0db3-4b7c-9547-f77f5586410b"
        }
      ],
      "cookie": [
      ],
      "body": "[\n    {\n        \"status\": \"verified\",\n        \"original_address\": {\n            \"name\": null,\n            \"phone\": null,\n            \"company_name\": null,\n            \"address_line1\": \"525 S Winchester Blvd\",\n            \"address_line2\": null,\n            \"address_line3\": null,\n            \"city_locality\": \"Austin\",\n            \"state_province\": \"Texas\",\n            \"postal_code\": \"95128\",\n            \"country_code\": \"US\",\n            \"address_residential_indicator\": \"unknown\"\n        },\n        \"matched_address\": {\n            \"name\": null,\n            \"phone\": null,\n            \"company_name\": null,\n            \"address_line1\": \"525 S WINCHESTER BLVD\",\n            \"address_line2\": \"\",\n            \"address_line3\": null,\n            \"city_locality\": \"SAN JOSE\",\n            \"state_province\": \"CA\",\n            \"postal_code\": \"95128-2537\",\n            \"country_code\": \"US\",\n            \"address_residential_indicator\": \"no\"\n        },\n        \"messages\": []\n    }\n]"
    }
  ]
}