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
(Visual FoxPro) UPS Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
LOCAL lnSuccess LOCAL loHttp LOCAL lcUrl LOCAL loJson LOCAL loSb LOCAL loResp LOCAL lcCustomerContext LOCAL lcStatusCode LOCAL lcStatusDescription LOCAL lcResultRank LOCAL lcResultQuality LOCAL lcCity LOCAL lcProvinceCode LOCAL lcPostalCodeLowEnd LOCAL lcPostalCodeHighEnd LOCAL lcRank LOCAL lcQuality LOCAL lcAddressCity LOCAL lcAddressStateProvinceCode LOCAL lnNumResults LOCAL i * This example requires the Chilkat API to have been previously unlocked. * See Global Unlock Sample for sample code. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.Http') loHttp = CreateObject('Chilkat.Http') * This is the testing endpoint for address validation: 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. * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.JsonObject') loJson = CreateObject('Chilkat.JsonObject') 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") * For versions of Chilkat < 10.0.0, use CreateObject('Chilkat_9_5_0.StringBuilder') loSb = CreateObject('Chilkat.StringBuilder') loResp = loHttp.PostJson3(lcUrl,"application/json",loJson) IF (loHttp.LastMethodSuccess <> 1) THEN ? loHttp.LastErrorText RELEASE loHttp RELEASE loJson RELEASE loSb CANCEL 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 CANCEL ENDIF loJson.Load(loResp.BodyStr) loJson.EmitCompact = 0 ? 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.