Chilkat Online Tools

Swift3 / ShipEngine Walkthrough / Validate an address (incomplete)

Back to Collection Items

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

    let http = CkoHttp()!
    var success: Bool

    // 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"
    //   }
    // ]

    let jarr = CkoJsonArray()!

    jarr.addObject(at: -1)
    var jsonObj_1: CkoJsonObject? = jarr.object(at: jarr.size.intValue - 1)
    jsonObj_1!.update("address_line1", value: "525 Winchester")
    jsonObj_1!.update("postal_code", value: "95128")
    jsonObj_1!.update("country_code", value: "US")
    jsonObj_1 = nil

    http.setRequestHeader("API-Key", value: "{{API_KEY}}")
    http.setRequestHeader("Content-Type", value: "application/json")

    let sbRequestBody = CkoStringBuilder()!
    jarr.emitSb(sbRequestBody)

    var resp: CkoHttpResponse? = http.pTextSb("POST", url: "https://api.shipengine.com/v1/addresses/validate", textData: sbRequestBody, charset: "utf-8", contentType: "application/json", md5: false, gzip: false)
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    let sbResponseBody = CkoStringBuilder()!
    resp!.getBodySb(sbResponseBody)

    let jarrResp = CkoJsonArray()!
    jarrResp.loadSb(sbResponseBody)
    jarrResp.emitCompact = false

    print("Response Body:")
    print("\(jarrResp.emit()!)")

    var respStatusCode: Int = resp!.statusCode.intValue
    print("Response Status Code = \(respStatusCode)")
    if respStatusCode >= 400 {
        print("Response Header:")
        print("\(resp!.header!)")
        print("Failed.")
        resp = nil
        return
    }

    resp = nil

    // 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

    var json: CkoJsonObject?
    var status: String?
    var Name: String?
    var Phone: String?
    var Company_name: String?
    var Address_line1: String?
    var Address_line2: String?
    var Address_line3: String?
    var City_locality: String?
    var State_province: String?
    var Postal_code: String?
    var Country_code: String?
    var Address_residential_indicator: String?
    var matched_addressName: String?
    var matched_addressPhone: String?
    var matched_addressCompany_name: String?
    var matched_addressAddress_line1: String?
    var matched_addressAddress_line2: String?
    var matched_addressAddress_line3: String?
    var matched_addressCity_locality: String?
    var matched_addressState_province: String?
    var matched_addressPostal_code: String?
    var matched_addressCountry_code: String?
    var matched_addressAddress_residential_indicator: String?
    var j: Int
    var count_j: Int

    var i: Int = 0
    var count_i: Int = jarrResp.size.intValue
    while i < count_i {
        json = jarrResp.object(at: i)
        status = json!.string(of: "status")
        Name = json!.string(of: "original_address.name")
        Phone = json!.string(of: "original_address.phone")
        Company_name = json!.string(of: "original_address.company_name")
        Address_line1 = json!.string(of: "original_address.address_line1")
        Address_line2 = json!.string(of: "original_address.address_line2")
        Address_line3 = json!.string(of: "original_address.address_line3")
        City_locality = json!.string(of: "original_address.city_locality")
        State_province = json!.string(of: "original_address.state_province")
        Postal_code = json!.string(of: "original_address.postal_code")
        Country_code = json!.string(of: "original_address.country_code")
        Address_residential_indicator = json!.string(of: "original_address.address_residential_indicator")
        matched_addressName = json!.string(of: "matched_address.name")
        matched_addressPhone = json!.string(of: "matched_address.phone")
        matched_addressCompany_name = json!.string(of: "matched_address.company_name")
        matched_addressAddress_line1 = json!.string(of: "matched_address.address_line1")
        matched_addressAddress_line2 = json!.string(of: "matched_address.address_line2")
        matched_addressAddress_line3 = json!.string(of: "matched_address.address_line3")
        matched_addressCity_locality = json!.string(of: "matched_address.city_locality")
        matched_addressState_province = json!.string(of: "matched_address.state_province")
        matched_addressPostal_code = json!.string(of: "matched_address.postal_code")
        matched_addressCountry_code = json!.string(of: "matched_address.country_code")
        matched_addressAddress_residential_indicator = json!.string(of: "matched_address.address_residential_indicator")
        j = 0
        count_j = json!.size(ofArray: "messages").intValue
        while j < count_j {
            json.j = j
            j = j + 1
        }

        json = nil
        i = i + 1
    }


}

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]"
    }
  ]
}