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

Delphi DLL 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

 

 

 

(Delphi DLL) UPS Address Validation (City, State, Zip)

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

Chilkat for Delphi Downloads

Chilkat non-ActiveX DLL for Delphi

Chilkat ActiveX DLL for Delphi

* The examples here use the non-ActiveX DLL.

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, StringBuilder, HttpResponse, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
url: PWideChar;
json: HCkJsonObject;
sb: HCkStringBuilder;
resp: HCkHttpResponse;
customerContext: PWideChar;
statusCode: PWideChar;
statusDescription: PWideChar;
resultRank: PWideChar;
resultQuality: PWideChar;
city: PWideChar;
provinceCode: PWideChar;
postalCodeLowEnd: PWideChar;
postalCodeHighEnd: PWideChar;
rank: PWideChar;
quality: PWideChar;
addressCity: PWideChar;
addressStateProvinceCode: PWideChar;
numResults: Integer;
i: Integer;

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

http := CkHttp_Create();

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

sb := CkStringBuilder_Create();
resp := CkHttp_PostJson3(http,url,'application/json',json);
if (CkHttp_getLastMethodSuccess(http) <> True) then
  begin
    Memo1.Lines.Add(CkHttp__lastErrorText(http));
    Exit;
  end;

Memo1.Lines.Add('status = ' + IntToStr(CkHttpResponse_getStatusCode(resp)));

// A 200 response status indicate success.
if (CkHttpResponse_getStatusCode(resp) <> 200) then
  begin
    Memo1.Lines.Add(CkHttpResponse__bodyStr(resp));
    Memo1.Lines.Add('Failed.');
    CkHttpResponse_Dispose(resp);
    Exit;
  end;

CkJsonObject_Load(json,CkHttpResponse__bodyStr(resp));
CkJsonObject_putEmitCompact(json,False);
Memo1.Lines.Add(CkJsonObject__emit(json));

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

numResults := CkJsonObject_SizeOfArray(json,'AddressValidationResponse.AddressValidationResult');
if (numResults < 0) then
  begin

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

    Memo1.Lines.Add('Exact match!');
    Memo1.Lines.Add('postal code: ' + postalCodeLowEnd);

  end
else
  begin

    Memo1.Lines.Add('Non-Exact match.');

    customerContext := CkJsonObject__stringOf(json,'AddressValidationResponse.Response.TransactionReference.CustomerContext');
    statusCode := CkJsonObject__stringOf(json,'AddressValidationResponse.Response.ResponseStatusCode');
    statusDescription := CkJsonObject__stringOf(json,'AddressValidationResponse.Response.ResponseStatusDescription');
    i := 0;
    while i < numResults do
      begin
        CkJsonObject_putI(json,i);
        rank := CkJsonObject__stringOf(json,'AddressValidationResponse.AddressValidationResult[i].Rank');
        Memo1.Lines.Add('rank: ' + rank);
        quality := CkJsonObject__stringOf(json,'AddressValidationResponse.AddressValidationResult[i].Quality');
        addressCity := CkJsonObject__stringOf(json,'AddressValidationResponse.AddressValidationResult[i].Address.City');
        Memo1.Lines.Add('addressCity: ' + addressCity);
        addressStateProvinceCode := CkJsonObject__stringOf(json,'AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode');
        postalCodeLowEnd := CkJsonObject__stringOf(json,'AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd');
        Memo1.Lines.Add('postal code: ' + postalCodeLowEnd);
        postalCodeHighEnd := CkJsonObject__stringOf(json,'AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd');
        i := i + 1;
      end;

  end;

CkHttpResponse_Dispose(resp);

CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkStringBuilder_Dispose(sb);

end;

 

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