Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
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
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) UPS Address Validation (City, State, Zip)

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    String sUrl
    Variant vJson
    Handle hoJson
    Handle hoSb
    Variant vResp
    Handle hoResp
    String sCustomerContext
    String sStatusCode
    String sStatusDescription
    String sResultRank
    String sResultQuality
    String sCity
    String sProvinceCode
    String sPostalCodeLowEnd
    String sPostalCodeHighEnd
    String sRank
    String sQuality
    String sAddressCity
    String sAddressStateProvinceCode
    Integer iNumResults
    Integer i
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    // This example requires 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

    // This is the testing endpoint for address validation:
    Move "https://wwwcie.ups.com/rest/AV" To sUrl

    // 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.
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get ComUpdateString Of hoJson "AccessRequest.AccessLicenseNumber" "UPS_ACCESS_KEY" To iSuccess
    Get ComUpdateString Of hoJson "AccessRequest.UserId" "UPS_USERNAME" To iSuccess
    Get ComUpdateString Of hoJson "AccessRequest.Password" "UPS_PASSWORD" To iSuccess
    Get ComUpdateString Of hoJson "AddressValidationRequest.Request.TransactionReference.CustomerContext" "Your Customer Context" To iSuccess
    Get ComUpdateString Of hoJson "AddressValidationRequest.Request.RequestAction" "AV" To iSuccess
    Get ComUpdateString Of hoJson "AddressValidationRequest.Address.City" "ALPHARETTA" To iSuccess
    // We're making an intentional mistake here by passing CA instead of GA.
    Get ComUpdateString Of hoJson "AddressValidationRequest.Address.StateProvinceCode" "CA" To iSuccess
    Get ComUpdateString Of hoJson "AddressValidationRequest.Address.PostalCode" "30005" To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get pvComObject of hoJson to vJson
    Get ComPostJson3 Of hoHttp sUrl "application/json" vJson 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 <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Showln "status = " iTemp1

    // A 200 response status indicate success.
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 <> 200) Begin
        Get ComBodyStr Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Send Destroy of hoResp
        Procedure_Return
    End

    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoJson sTemp1 To iSuccess
    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1

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

    Get ComSizeOfArray Of hoJson "AddressValidationResponse.AddressValidationResult" To iNumResults
    If (iNumResults < 0) Begin

        // Here's parse code for the above JSON exact response:
        Get ComStringOf Of hoJson "AddressValidationResponse.Response.TransactionReference.CustomerContext" To sCustomerContext
        Get ComStringOf Of hoJson "AddressValidationResponse.Response.ResponseStatusCode" To sStatusCode
        Get ComStringOf Of hoJson "AddressValidationResponse.Response.ResponseStatusDescription" To sStatusDescription
        Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.Rank" To sResultRank
        Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.Quality" To sResultQuality
        Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.Address.City" To sCity
        Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode" To sProvinceCode
        Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd" To sPostalCodeLowEnd
        Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd" To sPostalCodeHighEnd

        Showln "Exact match!"
        Showln "postal code: " sPostalCodeLowEnd

    End
    Else Begin

        Showln "Non-Exact match."

        Get ComStringOf Of hoJson "AddressValidationResponse.Response.TransactionReference.CustomerContext" To sCustomerContext
        Get ComStringOf Of hoJson "AddressValidationResponse.Response.ResponseStatusCode" To sStatusCode
        Get ComStringOf Of hoJson "AddressValidationResponse.Response.ResponseStatusDescription" To sStatusDescription
        Move 0 To i
        While (i < iNumResults)
            Set ComI Of hoJson To i
            Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].Rank" To sRank
            Showln "rank: " sRank
            Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].Quality" To sQuality
            Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].Address.City" To sAddressCity
            Showln "addressCity: " sAddressCity
            Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode" To sAddressStateProvinceCode
            Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd" To sPostalCodeLowEnd
            Showln "postal code: " sPostalCodeLowEnd
            Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd" To sPostalCodeHighEnd
            Move (i + 1) To i
        Loop

    End

    Send Destroy of hoResp


End_Procedure

 

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