Chilkat Online Tools

PowerBuilder / ShipEngine Walkthrough / Validate an address (warning)

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
string ls_Code
string ls_Message
string ls_V_type
string ls_Detail_code
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": "Studio Tour Drive, Leavesden WD25 7LR, UK",
//     "country_code": "GB"
//   }
// ]

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","Studio Tour Drive, Leavesden WD25 7LR, UK")
loo_JsonObj_1.UpdateString("country_code","GB")
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": "Studio Tour Drive, Leavesden WD25 7LR, UK",
//       "address_line2": null,
//       "address_line3": null,
//       "city_locality": null,
//       "state_province": null,
//       "postal_code": null,
//       "country_code": "GB",
//       "address_residential_indicator": "unknown"
//     },
//     "matched_address": {
//       "name": null,
//       "phone": null,
//       "company_name": "WARNER BROS STUDIO TOUR",
//       "address_line1": "Studio Tour Drive",
//       "address_line2": "",
//       "address_line3": "",
//       "city_locality": "Watford",
//       "state_province": "Hertfordshire",
//       "postal_code": "WD25 7LR",
//       "country_code": "GB",
//       "address_residential_indicator": "unknown"
//     },
//     "messages": [
//       {
//         "code": "a1003",
//         "message": "There was a change or addition to the state/province.",
//         "type": "warning",
//         "detail_code": "state_province_changed_or_added"
//       },
//       {
//         "code": "a1007",
//         "message": "This address has been verified down to the suite/PO box level (highest possible accuracy with the provided data)",
//         "type": "info",
//         "detail_code": "verified_to_suite_level"
//       },
//       {
//         "code": "a1008",
//         "message": "This record was successfully geocoded to the community level (ZIP centroid in the US, 3 digit postal code for Canada)",
//         "type": "info",
//         "detail_code": "coded_to_community_level"
//       }
//     ]
//   }
// ]

// 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
        ls_Code = loo_Json.StringOf("messages[j].code")
        ls_Message = loo_Json.StringOf("messages[j].message")
        ls_V_type = loo_Json.StringOf("messages[j].type")
        ls_Detail_code = loo_Json.StringOf("messages[j].detail_code")
        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": "Studio Tour Drive, Leavesden WD25 7LR, UK",
      "country_code": "GB"
	}
]'
https://api.shipengine.com/v1/addresses/validate

Postman Collection Item JSON

{
  "name": "Validate an address (warning)",
  "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      \"address_line1\": \"Studio Tour Drive, Leavesden WD25 7LR, UK\",\n      \"country_code\": \"GB\"\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 shows how ShipEngine responds when an address can be verified, but there are warnings such as major corrections. The `status` field is `verified`, and the `messages` array lists the warnings and other information about the corrected the address."
  },
  "response": [
    {
      "name": "Validate an address (warning)",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "name": "Content-Type",
            "type": "text",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "[\n\t{\n      \"address_line1\": \"Studio Tour Drive, Leavesden WD25 7LR, UK\",\n      \"country_code\": \"GB\"\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": "Tue, 22 Oct 2019 20:32:43 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "1614"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "12327571-2884-4f7d-bb9f-ad2cf9331d98"
        }
      ],
      "cookie": [
      ],
      "body": "[\n    {\n        \"status\": \"verified\",\n        \"original_address\": {\n            \"name\": null,\n            \"phone\": null,\n            \"company_name\": null,\n            \"address_line1\": \"Studio Tour Drive, Leavesden WD25 7LR, UK\",\n            \"address_line2\": null,\n            \"address_line3\": null,\n            \"city_locality\": null,\n            \"state_province\": null,\n            \"postal_code\": null,\n            \"country_code\": \"GB\",\n            \"address_residential_indicator\": \"unknown\"\n        },\n        \"matched_address\": {\n            \"name\": null,\n            \"phone\": null,\n            \"company_name\": \"WARNER BROS STUDIO TOUR\",\n            \"address_line1\": \"Studio Tour Drive\",\n            \"address_line2\": \"\",\n            \"address_line3\": \"\",\n            \"city_locality\": \"Watford\",\n            \"state_province\": \"Hertfordshire\",\n            \"postal_code\": \"WD25 7LR\",\n            \"country_code\": \"GB\",\n            \"address_residential_indicator\": \"unknown\"\n        },\n        \"messages\": [\n            {\n                \"code\": \"a1003\",\n                \"message\": \"There was a change or addition to the state/province.\",\n                \"type\": \"warning\",\n                \"detail_code\": \"state_province_changed_or_added\"\n            },\n            {\n                \"code\": \"a1007\",\n                \"message\": \"This address has been verified down to the suite/PO box level (highest possible accuracy with the provided data)\",\n                \"type\": \"info\",\n                \"detail_code\": \"verified_to_suite_level\"\n            },\n            {\n                \"code\": \"a1008\",\n                \"message\": \"This record was successfully geocoded to the community level (ZIP centroid in the US, 3 digit postal code for Canada)\",\n                \"type\": \"info\",\n                \"detail_code\": \"coded_to_community_level\"\n            }\n        ]\n    }\n]"
    }
  ]
}