Chilkat Online Tools

DataFlex / ShipEngine Walkthrough / Validate an address (unverified)

Back to Collection Items

Use ChilkatAx-9.5.0-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_address
    Integer j
    Integer iCount_j
    String sCode
    String sMessage
    String sV_type
    String sDetail_code
    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 S Winchester Blvd",
    //     "city_locality": "San Jose",
    //     "state_province": "CA",
    //     "postal_code": "95128",
    //     "country_code": "USA"
    //   }
    // ]

    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 S Winchester Blvd" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "city_locality" "San Jose" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "state_province" "CA" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "postal_code" "95128" To iSuccess
    Get ComUpdateString Of hoJsonObj_1 "country_code" "USA" 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": "unverified",
    //     "original_address": {
    //       "name": null,
    //       "phone": null,
    //       "company_name": null,
    //       "address_line1": "525 S Winchester Blvd",
    //       "address_line2": null,
    //       "address_line3": null,
    //       "city_locality": "San Jose",
    //       "state_province": "CA",
    //       "postal_code": "95128",
    //       "country_code": "USA",
    //       "address_residential_indicator": "unknown"
    //     },
    //     "matched_address": null,
    //     "messages": [
    //       {
    //         "code": "a1005",
    //         "message": "Length of property 'country_code' must equal exactly 2",
    //         "type": "error",
    //         "detail_code": "country_invalid_length"
    //       },
    //       {
    //         "code": "a1001",
    //         "message": "country_code is not currently a supported country, please check the documentation for acceptable 2-letter country codes.",
    //         "type": "error",
    //         "detail_code": "unsupported_country"
    //       }
    //     ]
    //   }
    // ]

    // 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" To sMatched_address
        Move 0 To j
        Get ComSizeOfArray Of hoJson "messages" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoJson To j
            Get ComStringOf Of hoJson "messages[j].code" To sCode
            Get ComStringOf Of hoJson "messages[j].message" To sMessage
            Get ComStringOf Of hoJson "messages[j].type" To sV_type
            Get ComStringOf Of hoJson "messages[j].detail_code" To sDetail_code
            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 S Winchester Blvd",
		"city_locality": "San Jose",
		"state_province": "CA",
		"postal_code": "95128",
		"country_code": "USA"
	}
]'
https://api.shipengine.com/v1/addresses/validate

Postman Collection Item JSON

{
  "name": "Validate an address (unverified)",
  "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\": \"San Jose\",\n\t\t\"state_province\": \"CA\",\n\t\t\"postal_code\": \"95128\",\n\t\t\"country_code\": \"USA\"\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 cannot be verified. The `status` field is `unverified`, and the `messages` array lists the problems with the address."
  },
  "response": [
    {
      "name": "Validate an address (unverified)",
      "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\": \"San Jose\",\n\t\t\"state_province\": \"CA\",\n\t\t\"postal_code\": \"95128\",\n\t\t\"country_code\": \"USA\"\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:28:48 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json; charset=utf-8"
        },
        {
          "key": "Content-Length",
          "value": "951"
        },
        {
          "key": "Connection",
          "value": "keep-alive"
        },
        {
          "key": "Access-Control-Allow-Origin",
          "value": "https://www.shipengine.com"
        },
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "x-shipengine-requestid",
          "value": "118b96e8-1e29-4ded-ba33-62a661e55038"
        }
      ],
      "cookie": [
      ],
      "body": "[\n    {\n        \"status\": \"unverified\",\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\": \"San Jose\",\n            \"state_province\": \"CA\",\n            \"postal_code\": \"95128\",\n            \"country_code\": \"USA\",\n            \"address_residential_indicator\": \"unknown\"\n        },\n        \"matched_address\": null,\n        \"messages\": [\n            {\n                \"code\": \"a1005\",\n                \"message\": \"Length of property 'country_code' must equal exactly 2\",\n                \"type\": \"error\",\n                \"detail_code\": \"country_invalid_length\"\n            },\n            {\n                \"code\": \"a1001\",\n                \"message\": \"country_code is not currently a supported country, please check the documentation for acceptable 2-letter country codes.\",\n                \"type\": \"error\",\n                \"detail_code\": \"unsupported_country\"\n            }\n        ]\n    }\n]"
    }
  ]
}