DataFlex / ShipEngine Walkthrough / Validate an address (incomplete)
Back to Collection Items
Use ChilkatAx-win32.pkg
Procedure Test
Handle hoHttp
Boolean iSuccess
Handle hoJarr
Variant vJsonObj_1
Handle hoJsonObj_1
Variant vSbRequestBody
Handle hoSbRequestBody
Variant vResp
Handle hoResp
Variant vSbResponseBody
Handle hoSbResponseBody
Handle hoJarrResp
Integer iRespStatusCode
Variant vJson
Handle hoJson
String sStatus
String sName
String sPhone
String sCompany_name
String sAddress_line1
String sAddress_line2
String sAddress_line3
String sCity_locality
String sState_province
String sPostal_code
String sCountry_code
String sAddress_residential_indicator
String sMatched_addressName
String sMatched_addressPhone
String sMatched_addressCompany_name
String sMatched_addressAddress_line1
String sMatched_addressAddress_line2
String sMatched_addressAddress_line3
String sMatched_addressCity_locality
String sMatched_addressState_province
String sMatched_addressPostal_code
String sMatched_addressCountry_code
String sMatched_addressAddress_residential_indicator
Integer j
Integer iCount_j
Integer i
Integer iCount_i
String sTemp1
Integer iTemp1
Boolean bTemp1
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
// 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 Winchester",
// "postal_code": "95128",
// "country_code": "US"
// }
// ]
Get Create (RefClass(cComChilkatJsonArray)) To hoJarr
If (Not(IsComObjectCreated(hoJarr))) Begin
Send CreateComObject of hoJarr
End
Get ComAddObjectAt Of hoJarr -1 To iSuccess
Get ComSize Of hoJarr To iTemp1
Get ComObjectAt Of hoJarr (iTemp1 - 1) To vJsonObj_1
If (IsComObject(vJsonObj_1)) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonObj_1
Set pvComObject Of hoJsonObj_1 To vJsonObj_1
End
Get ComUpdateString Of hoJsonObj_1 "address_line1" "525 Winchester" To iSuccess
Get ComUpdateString Of hoJsonObj_1 "postal_code" "95128" To iSuccess
Get ComUpdateString Of hoJsonObj_1 "country_code" "US" To iSuccess
Send Destroy of hoJsonObj_1
Send ComSetRequestHeader To hoHttp "API-Key" "{{API_KEY}}"
Send ComSetRequestHeader To hoHttp "Content-Type" "application/json"
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbRequestBody
If (Not(IsComObjectCreated(hoSbRequestBody))) Begin
Send CreateComObject of hoSbRequestBody
End
Get pvComObject of hoSbRequestBody to vSbRequestBody
Get ComEmitSb Of hoJarr vSbRequestBody To iSuccess
Get pvComObject of hoSbRequestBody to vSbRequestBody
Get ComPTextSb Of hoHttp "POST" "https://api.shipengine.com/v1/addresses/validate" vSbRequestBody "utf-8" "application/json" False False To vResp
If (IsComObject(vResp)) Begin
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
Set pvComObject Of hoResp To vResp
End
Get ComLastMethodSuccess Of hoHttp To bTemp1
If (bTemp1 = False) Begin
Get ComLastErrorText Of hoHttp To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
Send CreateComObject of hoSbResponseBody
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess
Get Create (RefClass(cComChilkatJsonArray)) To hoJarrResp
If (Not(IsComObjectCreated(hoJarrResp))) Begin
Send CreateComObject of hoJarrResp
End
Get pvComObject of hoSbResponseBody to vSbResponseBody
Get ComLoadSb Of hoJarrResp vSbResponseBody To iSuccess
Set ComEmitCompact Of hoJarrResp To False
Showln "Response Body:"
Get ComEmit Of hoJarrResp To sTemp1
Showln sTemp1
Get ComStatusCode Of hoResp To iRespStatusCode
Showln "Response Status Code = " iRespStatusCode
If (iRespStatusCode >= 400) Begin
Showln "Response Header:"
Get ComHeader Of hoResp To sTemp1
Showln sTemp1
Showln "Failed."
Send Destroy of hoResp
Procedure_Return
End
Send Destroy of hoResp
// 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 Winchester",
// "address_line2": null,
// "address_line3": null,
// "city_locality": null,
// "state_province": null,
// "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
Move 0 To i
Get ComSize Of hoJarrResp To iCount_i
While (i < iCount_i)
Get ComObjectAt Of hoJarrResp i To vJson
If (IsComObject(vJson)) Begin
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
Set pvComObject Of hoJson To vJson
End
Get ComStringOf Of hoJson "status" To sStatus
Get ComStringOf Of hoJson "original_address.name" To sName
Get ComStringOf Of hoJson "original_address.phone" To sPhone
Get ComStringOf Of hoJson "original_address.company_name" To sCompany_name
Get ComStringOf Of hoJson "original_address.address_line1" To sAddress_line1
Get ComStringOf Of hoJson "original_address.address_line2" To sAddress_line2
Get ComStringOf Of hoJson "original_address.address_line3" To sAddress_line3
Get ComStringOf Of hoJson "original_address.city_locality" To sCity_locality
Get ComStringOf Of hoJson "original_address.state_province" To sState_province
Get ComStringOf Of hoJson "original_address.postal_code" To sPostal_code
Get ComStringOf Of hoJson "original_address.country_code" To sCountry_code
Get ComStringOf Of hoJson "original_address.address_residential_indicator" To sAddress_residential_indicator
Get ComStringOf Of hoJson "matched_address.name" To sMatched_addressName
Get ComStringOf Of hoJson "matched_address.phone" To sMatched_addressPhone
Get ComStringOf Of hoJson "matched_address.company_name" To sMatched_addressCompany_name
Get ComStringOf Of hoJson "matched_address.address_line1" To sMatched_addressAddress_line1
Get ComStringOf Of hoJson "matched_address.address_line2" To sMatched_addressAddress_line2
Get ComStringOf Of hoJson "matched_address.address_line3" To sMatched_addressAddress_line3
Get ComStringOf Of hoJson "matched_address.city_locality" To sMatched_addressCity_locality
Get ComStringOf Of hoJson "matched_address.state_province" To sMatched_addressState_province
Get ComStringOf Of hoJson "matched_address.postal_code" To sMatched_addressPostal_code
Get ComStringOf Of hoJson "matched_address.country_code" To sMatched_addressCountry_code
Get ComStringOf Of hoJson "matched_address.address_residential_indicator" To sMatched_addressAddress_residential_indicator
Move 0 To j
Get ComSizeOfArray Of hoJson "messages" To iCount_j
While (j < iCount_j)
Set ComJ Of hoJson To j
Move (j + 1) To j
Loop
Send Destroy of hoJson
Move (i + 1) To i
Loop
End_Procedure
Curl Command
curl -X POST
-H "API-Key: {{API_KEY}}"
-H "Content-Type: application/json"
-d '[
{
"address_line1": "525 Winchester",
"postal_code": "95128",
"country_code": "US"
}
]'
https://api.shipengine.com/v1/addresses/validate
Postman Collection Item JSON
{
"name": "Validate an address (incomplete)",
"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 Winchester\",\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 validate and fill-in an incomplete address. For most addresses, a `postal_code` is sufficient to fill-in the `city_locality` and `state_province`, or vice-versa. Also notice that the incomplete stree address (525 Winchester) is filled-in as well (525 S Winchester Blvd)."
},
"response": [
{
"name": "Validate an address (incomplete)",
"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 Winchester\",\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:10:26 GMT"
},
{
"key": "Content-Type",
"value": "application/json; charset=utf-8"
},
{
"key": "Content-Length",
"value": "831"
},
{
"key": "Connection",
"value": "keep-alive"
},
{
"key": "Access-Control-Allow-Origin",
"value": "https://www.shipengine.com"
},
{
"key": "Vary",
"value": "Origin"
},
{
"key": "x-shipengine-requestid",
"value": "9bf1b583-40ed-4715-92b7-0e6bd6cd374c"
}
],
"cookie": [
],
"body": "[\n {\n \"status\": \"verified\",\n \"original_address\": {\n \"name\": null,\n \"phone\": null,\n \"company_name\": null,\n \"address_line1\": \"525 Winchester\",\n \"address_line2\": null,\n \"address_line3\": null,\n \"city_locality\": null,\n \"state_province\": null,\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]"
}
]
}