Unicode C++
Unicode C++
UPS Address Validation (City, State, Zip)
See more HTTP Misc Examples
Demonstrates making a call to the UPS address validation REST API.Chilkat Unicode C++ Downloads
#include <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>
#include <CkHttpResponseW.h>
void ChilkatSample(void)
{
bool success = false;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkHttpW http;
// This is the testing endpoint for address validation:
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;
success = http.HttpJson(L"POST",url,json,L"application/json",resp);
if (success == false) {
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");
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;
}
}
}