Chilkat Examples

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

DataFlex Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
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

 

 

 

(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-9.5.0-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

    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-2019 Chilkat Software, Inc. All Rights Reserved.