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
(Lianja) UPS Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
// This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loHttp = createobject("CkHttp") lcUrl = "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. loJson = createobject("CkJsonObject") loJson.UpdateString("AccessRequest.AccessLicenseNumber","UPS_ACCESS_KEY") loJson.UpdateString("AccessRequest.UserId","UPS_USERNAME") loJson.UpdateString("AccessRequest.Password","UPS_PASSWORD") loJson.UpdateString("AddressValidationRequest.Request.TransactionReference.CustomerContext","Your Customer Context") loJson.UpdateString("AddressValidationRequest.Request.RequestAction","AV") loJson.UpdateString("AddressValidationRequest.Address.City","ALPHARETTA") // We're making an intentional mistake here by passing CA instead of GA. loJson.UpdateString("AddressValidationRequest.Address.StateProvinceCode","CA") loJson.UpdateString("AddressValidationRequest.Address.PostalCode","30005") loSb = createobject("CkStringBuilder") loResp = loHttp.PostJson3(lcUrl,"application/json",loJson) if (loHttp.LastMethodSuccess <> .T.) then ? loHttp.LastErrorText release loHttp release loJson release loSb return endif ? "status = " + str(loResp.StatusCode) // A 200 response status indicate success. if (loResp.StatusCode <> 200) then ? loResp.BodyStr ? "Failed." release loResp release loHttp release loJson release loSb return endif loJson.Load(loResp.BodyStr) loJson.EmitCompact = .F. ? loJson.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. lnNumResults = loJson.SizeOfArray("AddressValidationResponse.AddressValidationResult") if (lnNumResults < 0) then // Here's parse code for the above JSON exact response: lcCustomerContext = loJson.StringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext") lcStatusCode = loJson.StringOf("AddressValidationResponse.Response.ResponseStatusCode") lcStatusDescription = loJson.StringOf("AddressValidationResponse.Response.ResponseStatusDescription") lcResultRank = loJson.StringOf("AddressValidationResponse.AddressValidationResult.Rank") lcResultQuality = loJson.StringOf("AddressValidationResponse.AddressValidationResult.Quality") lcCity = loJson.StringOf("AddressValidationResponse.AddressValidationResult.Address.City") lcProvinceCode = loJson.StringOf("AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode") lcPostalCodeLowEnd = loJson.StringOf("AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd") lcPostalCodeHighEnd = loJson.StringOf("AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd") ? "Exact match!" ? "postal code: " + lcPostalCodeLowEnd else ? "Non-Exact match." lcCustomerContext = loJson.StringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext") lcStatusCode = loJson.StringOf("AddressValidationResponse.Response.ResponseStatusCode") lcStatusDescription = loJson.StringOf("AddressValidationResponse.Response.ResponseStatusDescription") i = 0 do while i < lnNumResults loJson.I = i lcRank = loJson.StringOf("AddressValidationResponse.AddressValidationResult[i].Rank") ? "rank: " + lcRank lcQuality = loJson.StringOf("AddressValidationResponse.AddressValidationResult[i].Quality") lcAddressCity = loJson.StringOf("AddressValidationResponse.AddressValidationResult[i].Address.City") ? "addressCity: " + lcAddressCity lcAddressStateProvinceCode = loJson.StringOf("AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode") lcPostalCodeLowEnd = loJson.StringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd") ? "postal code: " + lcPostalCodeLowEnd lcPostalCodeHighEnd = loJson.StringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd") i = i + 1 enddo endif release loResp release loHttp release loJson release loSb |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.