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

Unicode C++ 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
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

 

 

 

(Unicode C++) UPS Address Validation (City, State, Zip)

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

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Solaris C/C++

MinGW C/C++

#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
#include <CkHttpResponseW.h>

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

    bool success;

    CkHttpW http;

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

    CkStringBuilderW sb;
    CkHttpResponseW *resp = http.PostJson3(url,L"application/json",json);
    if (http.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",http.lastErrorText());
        return;
    }

    wprintf(L"status = %d\n",resp->get_StatusCode());

    // A 200 response status indicate success.
    if (resp->get_StatusCode() != 200) {
        wprintf(L"%s\n",resp->bodyStr());
        wprintf(L"Failed.\n");
        delete resp;
        return;
    }

    json.Load(resp->bodyStr());
    json.put_EmitCompact(false);
    wprintf(L"%s\n",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.

    const wchar_t *customerContext = 0;
    const wchar_t *statusCode = 0;
    const wchar_t *statusDescription = 0;
    const wchar_t *resultRank = 0;
    const wchar_t *resultQuality = 0;
    const wchar_t *city = 0;
    const wchar_t *provinceCode = 0;
    const wchar_t *postalCodeLowEnd = 0;
    const wchar_t *postalCodeHighEnd = 0;
    const wchar_t *rank = 0;
    const wchar_t *quality = 0;
    const wchar_t *addressCity = 0;
    const wchar_t *addressStateProvinceCode = 0;

    int numResults = json.SizeOfArray(L"AddressValidationResponse.AddressValidationResult");
    if (numResults < 0) {

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

        wprintf(L"Exact match!\n");
        wprintf(L"postal code: %s\n",postalCodeLowEnd);

    }
    else {

        wprintf(L"Non-Exact match.\n");

        customerContext = json.stringOf(L"AddressValidationResponse.Response.TransactionReference.CustomerContext");
        statusCode = json.stringOf(L"AddressValidationResponse.Response.ResponseStatusCode");
        statusDescription = json.stringOf(L"AddressValidationResponse.Response.ResponseStatusDescription");
        int i = 0;
        while (i < numResults) {
            json.put_I(i);
            rank = json.stringOf(L"AddressValidationResponse.AddressValidationResult[i].Rank");
            wprintf(L"rank: %s\n",rank);
            quality = json.stringOf(L"AddressValidationResponse.AddressValidationResult[i].Quality");
            addressCity = json.stringOf(L"AddressValidationResponse.AddressValidationResult[i].Address.City");
            wprintf(L"addressCity: %s\n",addressCity);
            addressStateProvinceCode = json.stringOf(L"AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode");
            postalCodeLowEnd = json.stringOf(L"AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd");
            wprintf(L"postal code: %s\n",postalCodeLowEnd);
            postalCodeHighEnd = json.stringOf(L"AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd");
            i = i + 1;
        }

    }

    delete resp;
    }

 

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