Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) UPS Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
#include <C_CkHttpW.h> #include <C_CkJsonObjectW.h> #include <C_CkStringBuilderW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { BOOL success; HCkHttpW http; const wchar_t *url; HCkJsonObjectW json; HCkStringBuilderW sb; HCkHttpResponseW resp; const wchar_t *customerContext; const wchar_t *statusCode; const wchar_t *statusDescription; const wchar_t *resultRank; const wchar_t *resultQuality; const wchar_t *city; const wchar_t *provinceCode; const wchar_t *postalCodeLowEnd; const wchar_t *postalCodeHighEnd; const wchar_t *rank; const wchar_t *quality; const wchar_t *addressCity; const wchar_t *addressStateProvinceCode; int numResults; int i; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. http = CkHttpW_Create(); 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. json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"AccessRequest.AccessLicenseNumber",L"UPS_ACCESS_KEY"); CkJsonObjectW_UpdateString(json,L"AccessRequest.UserId",L"UPS_USERNAME"); CkJsonObjectW_UpdateString(json,L"AccessRequest.Password",L"UPS_PASSWORD"); CkJsonObjectW_UpdateString(json,L"AddressValidationRequest.Request.TransactionReference.CustomerContext",L"Your Customer Context"); CkJsonObjectW_UpdateString(json,L"AddressValidationRequest.Request.RequestAction",L"AV"); CkJsonObjectW_UpdateString(json,L"AddressValidationRequest.Address.City",L"ALPHARETTA"); // We're making an intentional mistake here by passing CA instead of GA. CkJsonObjectW_UpdateString(json,L"AddressValidationRequest.Address.StateProvinceCode",L"CA"); CkJsonObjectW_UpdateString(json,L"AddressValidationRequest.Address.PostalCode",L"30005"); sb = CkStringBuilderW_Create(); resp = CkHttpW_PostJson3(http,url,L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); return; } wprintf(L"status = %d\n",CkHttpResponseW_getStatusCode(resp)); // A 200 response status indicate success. if (CkHttpResponseW_getStatusCode(resp) != 200) { wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); wprintf(L"Failed.\n"); CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); return; } CkJsonObjectW_Load(json,CkHttpResponseW_bodyStr(resp)); CkJsonObjectW_putEmitCompact(json,FALSE); wprintf(L"%s\n",CkJsonObjectW_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 = CkJsonObjectW_SizeOfArray(json,L"AddressValidationResponse.AddressValidationResult"); if (numResults < 0) { // Here's parse code for the above JSON exact response: customerContext = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.Response.TransactionReference.CustomerContext"); statusCode = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.Response.ResponseStatusCode"); statusDescription = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.Response.ResponseStatusDescription"); resultRank = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult.Rank"); resultQuality = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult.Quality"); city = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult.Address.City"); provinceCode = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode"); postalCodeLowEnd = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd"); postalCodeHighEnd = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd"); wprintf(L"Exact match!\n"); wprintf(L"postal code: %s\n",postalCodeLowEnd); } else { wprintf(L"Non-Exact match.\n"); customerContext = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.Response.TransactionReference.CustomerContext"); statusCode = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.Response.ResponseStatusCode"); statusDescription = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.Response.ResponseStatusDescription"); i = 0; while (i < numResults) { CkJsonObjectW_putI(json,i); rank = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult[i].Rank"); wprintf(L"rank: %s\n",rank); quality = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult[i].Quality"); addressCity = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult[i].Address.City"); wprintf(L"addressCity: %s\n",addressCity); addressStateProvinceCode = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode"); postalCodeLowEnd = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd"); wprintf(L"postal code: %s\n",postalCodeLowEnd); postalCodeHighEnd = CkJsonObjectW_stringOf(json,L"AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd"); i = i + 1; } } CkHttpResponseW_Dispose(resp); CkHttpW_Dispose(http); CkJsonObjectW_Dispose(json); CkStringBuilderW_Dispose(sb); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.