Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Swift 2 Examples

Web API Categories

ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip

HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
NTLM
OAuth1
OAuth2
Office365
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Swift 2) UPS Address Validation (City, State, Zip)

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

Chilkat Downloads for the Swift Programming Language

MAC OS X (Cocoa) Objective-C/Swift Libs

iOS Objective-C/Swift Libs

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

    var success: Bool

    let http = CkoHttp()

    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("AccessRequest.AccessLicenseNumber", value: "UPS_ACCESS_KEY")
    json.UpdateString("AccessRequest.UserId", value: "UPS_USERNAME")
    json.UpdateString("AccessRequest.Password", value: "UPS_PASSWORD")
    json.UpdateString("AddressValidationRequest.Request.TransactionReference.CustomerContext", value: "Your Customer Context")
    json.UpdateString("AddressValidationRequest.Request.RequestAction", value: "AV")
    json.UpdateString("AddressValidationRequest.Address.City", value: "ALPHARETTA")
    // We're making an intentional mistake here by passing CA instead of GA.
    json.UpdateString("AddressValidationRequest.Address.StateProvinceCode", value: "CA")
    json.UpdateString("AddressValidationRequest.Address.PostalCode", value: "30005")

    let sb = CkoStringBuilder()
    var resp: CkoHttpResponse? = http.PostJson3(url, contentType: "application/json", json: json)
    if http.LastMethodSuccess != true {
        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.")
        resp = nil
        return
    }

    json.Load(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.SizeOfArray("AddressValidationResponse.AddressValidationResult").intValue
    if numResults < 0 {

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

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

    }
    else {

        print("Non-Exact match.")

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

    }

    resp = nil

}

 

© 2000-2020 Chilkat Software, Inc. All Rights Reserved.