Sample code for 30+ languages & platforms
Swift

UPS Address Validation (City, State, Zip)

See more HTTP Misc Examples

Demonstrates making a call to the UPS address validation REST API.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

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

    let http = CkoHttp()!

    // This is the testing endpoint for address validation:
    var url: String? = "https://wwwcie.ups.com/rest/AV"

    // Send an HTTP request with the following JSON body:

    // {
    //   "AccessRequest": {
    //     "AccessLicenseNumber": "Your Access License Number",
    //     "UserId": "Your Username",
    //     "Password": "Your Password"
    //   },
    //   "AddressValidationRequest": {
    //     "Request": {
    //       "TransactionReference": {
    //         "CustomerContext": "Your Customer Context"
    //       },
    //       "RequestAction": "AV"
    //     },
    //     "Address": {
    //       "City": "ALPHARETTA",
    //       "StateProvinceCode": "GA",
    //       "PostalCode": "30005"
    //     }
    //   }
    // }

    // Build the above JSON.
    let json = CkoJsonObject()!
    json.updateString(jsonPath: "AccessRequest.AccessLicenseNumber", value: "UPS_ACCESS_KEY")
    json.updateString(jsonPath: "AccessRequest.UserId", value: "UPS_USERNAME")
    json.updateString(jsonPath: "AccessRequest.Password", value: "UPS_PASSWORD")
    json.updateString(jsonPath: "AddressValidationRequest.Request.TransactionReference.CustomerContext", value: "Your Customer Context")
    json.updateString(jsonPath: "AddressValidationRequest.Request.RequestAction", value: "AV")
    json.updateString(jsonPath: "AddressValidationRequest.Address.City", value: "ALPHARETTA")
    // We're making an intentional mistake here by passing CA instead of GA.
    json.updateString(jsonPath: "AddressValidationRequest.Address.StateProvinceCode", value: "CA")
    json.updateString(jsonPath: "AddressValidationRequest.Address.PostalCode", value: "30005")

    let sb = CkoStringBuilder()!
    let resp = CkoHttpResponse()!
    success = http.httpJson(verb: "POST", url: url, json: json, contentType: "application/json", response: resp)
    if success == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("status = \(resp.statusCode.intValue)")

    // A 200 response status indicate success.
    if resp.statusCode.intValue != 200 {
        print("\(resp.bodyStr!)")
        print("Failed.")
        return
    }

    json.load(json: resp.bodyStr)
    json.emitCompact = false
    print("\(json.emit()!)")

    // A successful exact response looks like this:

    // {
    //   "AddressValidationResponse": {
    //     "Response": {
    //       "TransactionReference": {
    //         "CustomerContext": "Your Customer Context"
    //       },
    //       "ResponseStatusCode": "1",
    //       "ResponseStatusDescription": "Success"
    //     },
    //     "AddressValidationResult": {
    //       "Rank": "1",
    //       "Quality": "1.0",
    //       "Address": {
    //         "City": "ALPHARETTA",
    //         "StateProvinceCode": "GA"
    //       },
    //       "PostalCodeLowEnd": "30005",
    //       "PostalCodeHighEnd": "30005"
    //     }
    //   }
    // }
    // 

    // A successful response that was not an exact match provides an array of closest matches, like this:

    // {
    //   "AddressValidationResponse": {
    //     "Response": {
    //       "TransactionReference": {
    //         "CustomerContext": "Your Customer Context"
    //         "Quality": "0.9875",
    //         "Address": {
    //       },
    //       "ResponseStatusCode": "1",
    //       "ResponseStatusDescription": "Success"
    //     },
    //     "AddressValidationResult": [
    //       {
    //         "Rank": "1",
    //           "City": "ALPHARETTA",
    //           "StateProvinceCode": "GA"
    //         },
    //         "PostalCodeLowEnd": "30005",
    //         "PostalCodeHighEnd": "30005"
    //       },
    //       {
    //         "Rank": "2",
    //         "Quality": "0.9750",
    //         "Address": {
    //           "City": "ALPHARETTA",
    //           "StateProvinceCode": "GA"
    //         },
    //         "PostalCodeLowEnd": "30004",
    //         "PostalCodeHighEnd": "30004"
    //       },
    //       {
    //         "Rank": "3",
    //         "Quality": "0.9750",
    //         "Address": {
    //           "City": "ALPHARETTA",
    //           "StateProvinceCode": "GA"
    //         },
    //         "PostalCodeLowEnd": "30009",
    //         "PostalCodeHighEnd": "30009"
    //       }
    //     ]
    //   }
    // }

    // Use the online tool at Generate JSON Parsing Code
    // to generate JSON parsing code.

    var customerContext: String?
    var statusCode: String?
    var statusDescription: String?
    var resultRank: String?
    var resultQuality: String?
    var city: String?
    var provinceCode: String?
    var postalCodeLowEnd: String?
    var postalCodeHighEnd: String?
    var rank: String?
    var quality: String?
    var addressCity: String?
    var addressStateProvinceCode: String?

    var numResults: Int = json.size(ofArray: "AddressValidationResponse.AddressValidationResult").intValue
    if numResults < 0 {

        // Here's parse code for the above JSON exact response:
        customerContext = json.string(of: "AddressValidationResponse.Response.TransactionReference.CustomerContext")
        statusCode = json.string(of: "AddressValidationResponse.Response.ResponseStatusCode")
        statusDescription = json.string(of: "AddressValidationResponse.Response.ResponseStatusDescription")
        resultRank = json.string(of: "AddressValidationResponse.AddressValidationResult.Rank")
        resultQuality = json.string(of: "AddressValidationResponse.AddressValidationResult.Quality")
        city = json.string(of: "AddressValidationResponse.AddressValidationResult.Address.City")
        provinceCode = json.string(of: "AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode")
        postalCodeLowEnd = json.string(of: "AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd")
        postalCodeHighEnd = json.string(of: "AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd")

        print("Exact match!")
        print("postal code: \(postalCodeLowEnd!)")

    }
    else {

        print("Non-Exact match.")

        customerContext = json.string(of: "AddressValidationResponse.Response.TransactionReference.CustomerContext")
        statusCode = json.string(of: "AddressValidationResponse.Response.ResponseStatusCode")
        statusDescription = json.string(of: "AddressValidationResponse.Response.ResponseStatusDescription")
        var i: Int = 0
        while i < numResults {
            json.i = i
            rank = json.string(of: "AddressValidationResponse.AddressValidationResult[i].Rank")
            print("rank: \(rank!)")
            quality = json.string(of: "AddressValidationResponse.AddressValidationResult[i].Quality")
            addressCity = json.string(of: "AddressValidationResponse.AddressValidationResult[i].Address.City")
            print("addressCity: \(addressCity!)")
            addressStateProvinceCode = json.string(of: "AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode")
            postalCodeLowEnd = json.string(of: "AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd")
            print("postal code: \(postalCodeLowEnd!)")
            postalCodeHighEnd = json.string(of: "AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd")
            i = i + 1
        }

    }


}