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
(C) UPS Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
#include <C_CkHttp.h> #include <C_CkJsonObject.h> #include <C_CkStringBuilder.h> #include <C_CkHttpResponse.h> void ChilkatSample(void) { BOOL success; HCkHttp http; const char *url; HCkJsonObject json; HCkStringBuilder sb; HCkHttpResponse resp; const char *customerContext; const char *statusCode; const char *statusDescription; const char *resultRank; const char *resultQuality; const char *city; const char *provinceCode; const char *postalCodeLowEnd; const char *postalCodeHighEnd; const char *rank; const char *quality; const char *addressCity; const char *addressStateProvinceCode; int numResults; int i; // 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) { printf("%s\n",CkHttp_lastErrorText(http)); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sb); return; } printf("status = %d\n",CkHttpResponse_getStatusCode(resp)); // A 200 response status indicate success. if (CkHttpResponse_getStatusCode(resp) != 200) { printf("%s\n",CkHttpResponse_bodyStr(resp)); printf("Failed.\n"); CkHttpResponse_Dispose(resp); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sb); return; } CkJsonObject_Load(json,CkHttpResponse_bodyStr(resp)); CkJsonObject_putEmitCompact(json,FALSE); printf("%s\n",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) { // 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"); printf("Exact match!\n"); printf("postal code: %s\n",postalCodeLowEnd); } else { printf("Non-Exact match.\n"); 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) { CkJsonObject_putI(json,i); rank = CkJsonObject_stringOf(json,"AddressValidationResponse.AddressValidationResult[i].Rank"); printf("rank: %s\n",rank); quality = CkJsonObject_stringOf(json,"AddressValidationResponse.AddressValidationResult[i].Quality"); addressCity = CkJsonObject_stringOf(json,"AddressValidationResponse.AddressValidationResult[i].Address.City"); printf("addressCity: %s\n",addressCity); addressStateProvinceCode = CkJsonObject_stringOf(json,"AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode"); postalCodeLowEnd = CkJsonObject_stringOf(json,"AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd"); printf("postal code: %s\n",postalCodeLowEnd); postalCodeHighEnd = CkJsonObject_stringOf(json,"AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd"); i = i + 1; } } CkHttpResponse_Dispose(resp); CkHttp_Dispose(http); CkJsonObject_Dispose(json); CkStringBuilder_Dispose(sb); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.