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
(DataFlex) UPS Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
Use ChilkatAx-win32.pkg Procedure Test Boolean iSuccess Handle hoHttp String sUrl Variant vJson Handle hoJson Handle hoSb Variant vResp Handle hoResp String sCustomerContext String sStatusCode String sStatusDescription String sResultRank String sResultQuality String sCity String sProvinceCode String sPostalCodeLowEnd String sPostalCodeHighEnd String sRank String sQuality String sAddressCity String sAddressStateProvinceCode Integer iNumResults Integer i String sTemp1 Integer iTemp1 Boolean bTemp1 // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. Get Create (RefClass(cComChilkatHttp)) To hoHttp If (Not(IsComObjectCreated(hoHttp))) Begin Send CreateComObject of hoHttp End // This is the testing endpoint for address validation: Move "https://wwwcie.ups.com/rest/AV" To sUrl // 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. Get Create (RefClass(cComChilkatJsonObject)) To hoJson If (Not(IsComObjectCreated(hoJson))) Begin Send CreateComObject of hoJson End Get ComUpdateString Of hoJson "AccessRequest.AccessLicenseNumber" "UPS_ACCESS_KEY" To iSuccess Get ComUpdateString Of hoJson "AccessRequest.UserId" "UPS_USERNAME" To iSuccess Get ComUpdateString Of hoJson "AccessRequest.Password" "UPS_PASSWORD" To iSuccess Get ComUpdateString Of hoJson "AddressValidationRequest.Request.TransactionReference.CustomerContext" "Your Customer Context" To iSuccess Get ComUpdateString Of hoJson "AddressValidationRequest.Request.RequestAction" "AV" To iSuccess Get ComUpdateString Of hoJson "AddressValidationRequest.Address.City" "ALPHARETTA" To iSuccess // We're making an intentional mistake here by passing CA instead of GA. Get ComUpdateString Of hoJson "AddressValidationRequest.Address.StateProvinceCode" "CA" To iSuccess Get ComUpdateString Of hoJson "AddressValidationRequest.Address.PostalCode" "30005" To iSuccess Get Create (RefClass(cComChilkatStringBuilder)) To hoSb If (Not(IsComObjectCreated(hoSb))) Begin Send CreateComObject of hoSb End Get pvComObject of hoJson to vJson Get ComPostJson3 Of hoHttp sUrl "application/json" vJson To vResp If (IsComObject(vResp)) Begin Get Create (RefClass(cComChilkatHttpResponse)) To hoResp Set pvComObject Of hoResp To vResp End Get ComLastMethodSuccess Of hoHttp To bTemp1 If (bTemp1 <> True) Begin Get ComLastErrorText Of hoHttp To sTemp1 Showln sTemp1 Procedure_Return End Get ComStatusCode Of hoResp To iTemp1 Showln "status = " iTemp1 // A 200 response status indicate success. Get ComStatusCode Of hoResp To iTemp1 If (iTemp1 <> 200) Begin Get ComBodyStr Of hoResp To sTemp1 Showln sTemp1 Showln "Failed." Send Destroy of hoResp Procedure_Return End Get ComBodyStr Of hoResp To sTemp1 Get ComLoad Of hoJson sTemp1 To iSuccess Set ComEmitCompact Of hoJson To False Get ComEmit Of hoJson To sTemp1 Showln sTemp1 // 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. Get ComSizeOfArray Of hoJson "AddressValidationResponse.AddressValidationResult" To iNumResults If (iNumResults < 0) Begin // Here's parse code for the above JSON exact response: Get ComStringOf Of hoJson "AddressValidationResponse.Response.TransactionReference.CustomerContext" To sCustomerContext Get ComStringOf Of hoJson "AddressValidationResponse.Response.ResponseStatusCode" To sStatusCode Get ComStringOf Of hoJson "AddressValidationResponse.Response.ResponseStatusDescription" To sStatusDescription Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.Rank" To sResultRank Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.Quality" To sResultQuality Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.Address.City" To sCity Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode" To sProvinceCode Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd" To sPostalCodeLowEnd Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd" To sPostalCodeHighEnd Showln "Exact match!" Showln "postal code: " sPostalCodeLowEnd End Else Begin Showln "Non-Exact match." Get ComStringOf Of hoJson "AddressValidationResponse.Response.TransactionReference.CustomerContext" To sCustomerContext Get ComStringOf Of hoJson "AddressValidationResponse.Response.ResponseStatusCode" To sStatusCode Get ComStringOf Of hoJson "AddressValidationResponse.Response.ResponseStatusDescription" To sStatusDescription Move 0 To i While (i < iNumResults) Set ComI Of hoJson To i Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].Rank" To sRank Showln "rank: " sRank Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].Quality" To sQuality Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].Address.City" To sAddressCity Showln "addressCity: " sAddressCity Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode" To sAddressStateProvinceCode Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd" To sPostalCodeLowEnd Showln "postal code: " sPostalCodeLowEnd Get ComStringOf Of hoJson "AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd" To sPostalCodeHighEnd Move (i + 1) To i Loop End Send Destroy of hoResp End_Procedure |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.