Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) UPS Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
#include <CkHttp.h> #include <CkJsonObject.h> #include <CkStringBuilder.h> #include <CkHttpResponse.h> void ChilkatSample(void) { CkString strOut; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. bool success; CkHttp http; const char *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. CkJsonObject json; json.UpdateString("AccessRequest.AccessLicenseNumber","UPS_ACCESS_KEY"); json.UpdateString("AccessRequest.UserId","UPS_USERNAME"); json.UpdateString("AccessRequest.Password","UPS_PASSWORD"); json.UpdateString("AddressValidationRequest.Request.TransactionReference.CustomerContext","Your Customer Context"); json.UpdateString("AddressValidationRequest.Request.RequestAction","AV"); json.UpdateString("AddressValidationRequest.Address.City","ALPHARETTA"); // We're making an intentional mistake here by passing CA instead of GA. json.UpdateString("AddressValidationRequest.Address.StateProvinceCode","CA"); json.UpdateString("AddressValidationRequest.Address.PostalCode","30005"); CkStringBuilder sb; CkHttpResponse *resp = http.PostJson3(url,"application/json",json); if (http.get_LastMethodSuccess() != true) { strOut.append(http.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } strOut.append("status = "); strOut.appendInt(resp->get_StatusCode()); strOut.append("\r\n"); // A 200 response status indicate success. if (resp->get_StatusCode() != 200) { strOut.append(resp->bodyStr()); strOut.append("\r\n"); strOut.append("Failed."); strOut.append("\r\n"); delete resp; SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } json.Load(resp->bodyStr()); json.put_EmitCompact(false); strOut.append(json.emit()); strOut.append("\r\n"); // 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 char *customerContext = 0; const char *statusCode = 0; const char *statusDescription = 0; const char *resultRank = 0; const char *resultQuality = 0; const char *city = 0; const char *provinceCode = 0; const char *postalCodeLowEnd = 0; const char *postalCodeHighEnd = 0; const char *rank = 0; const char *quality = 0; const char *addressCity = 0; const char *addressStateProvinceCode = 0; int numResults = json.SizeOfArray("AddressValidationResponse.AddressValidationResult"); if (numResults < 0) { // Here's parse code for the above JSON exact response: customerContext = json.stringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext"); statusCode = json.stringOf("AddressValidationResponse.Response.ResponseStatusCode"); statusDescription = json.stringOf("AddressValidationResponse.Response.ResponseStatusDescription"); resultRank = json.stringOf("AddressValidationResponse.AddressValidationResult.Rank"); resultQuality = json.stringOf("AddressValidationResponse.AddressValidationResult.Quality"); city = json.stringOf("AddressValidationResponse.AddressValidationResult.Address.City"); provinceCode = json.stringOf("AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode"); postalCodeLowEnd = json.stringOf("AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd"); postalCodeHighEnd = json.stringOf("AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd"); strOut.append("Exact match!"); strOut.append("\r\n"); strOut.append("postal code: "); strOut.append(postalCodeLowEnd); strOut.append("\r\n"); } else { strOut.append("Non-Exact match."); strOut.append("\r\n"); customerContext = json.stringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext"); statusCode = json.stringOf("AddressValidationResponse.Response.ResponseStatusCode"); statusDescription = json.stringOf("AddressValidationResponse.Response.ResponseStatusDescription"); int i = 0; while (i < numResults) { json.put_I(i); rank = json.stringOf("AddressValidationResponse.AddressValidationResult[i].Rank"); strOut.append("rank: "); strOut.append(rank); strOut.append("\r\n"); quality = json.stringOf("AddressValidationResponse.AddressValidationResult[i].Quality"); addressCity = json.stringOf("AddressValidationResponse.AddressValidationResult[i].Address.City"); strOut.append("addressCity: "); strOut.append(addressCity); strOut.append("\r\n"); addressStateProvinceCode = json.stringOf("AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode"); postalCodeLowEnd = json.stringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd"); strOut.append("postal code: "); strOut.append(postalCodeLowEnd); strOut.append("\r\n"); postalCodeHighEnd = json.stringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd"); i = i + 1; } } delete resp; SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.