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
(AutoIt) 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. Local $bSuccess $oHttp = ObjCreate("Chilkat.Http") ; This is the testing endpoint for address validation: Local $sUrl = "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. $oJson = ObjCreate("Chilkat.JsonObject") $oJson.UpdateString("AccessRequest.AccessLicenseNumber","UPS_ACCESS_KEY") $oJson.UpdateString("AccessRequest.UserId","UPS_USERNAME") $oJson.UpdateString("AccessRequest.Password","UPS_PASSWORD") $oJson.UpdateString("AddressValidationRequest.Request.TransactionReference.CustomerContext","Your Customer Context") $oJson.UpdateString("AddressValidationRequest.Request.RequestAction","AV") $oJson.UpdateString("AddressValidationRequest.Address.City","ALPHARETTA") ; We're making an intentional mistake here by passing CA instead of GA. $oJson.UpdateString("AddressValidationRequest.Address.StateProvinceCode","CA") $oJson.UpdateString("AddressValidationRequest.Address.PostalCode","30005") $oSb = ObjCreate("Chilkat.StringBuilder") Local $oResp = $oHttp.PostJson3($sUrl,"application/json",$oJson) If ($oHttp.LastMethodSuccess <> True) Then ConsoleWrite($oHttp.LastErrorText & @CRLF) Exit EndIf ConsoleWrite("status = " & $oResp.StatusCode & @CRLF) ; A 200 response status indicate success. If ($oResp.StatusCode <> 200) Then ConsoleWrite($oResp.BodyStr & @CRLF) ConsoleWrite("Failed." & @CRLF) Exit EndIf $oJson.Load($oResp.BodyStr) $oJson.EmitCompact = False ConsoleWrite($oJson.Emit() & @CRLF) ; 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. Local $sCustomerContext Local $statusCode Local $statusDescription Local $sResultRank Local $sResultQuality Local $sCity Local $sProvinceCode Local $sPostalCodeLowEnd Local $sPostalCodeHighEnd Local $sRank Local $sQuality Local $sAddressCity Local $sAddressStateProvinceCode Local $iNumResults = $oJson.SizeOfArray("AddressValidationResponse.AddressValidationResult") If ($iNumResults < 0) Then ; Here's parse code for the above JSON exact response: $sCustomerContext = $oJson.StringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext") $statusCode = $oJson.StringOf("AddressValidationResponse.Response.ResponseStatusCode") $statusDescription = $oJson.StringOf("AddressValidationResponse.Response.ResponseStatusDescription") $sResultRank = $oJson.StringOf("AddressValidationResponse.AddressValidationResult.Rank") $sResultQuality = $oJson.StringOf("AddressValidationResponse.AddressValidationResult.Quality") $sCity = $oJson.StringOf("AddressValidationResponse.AddressValidationResult.Address.City") $sProvinceCode = $oJson.StringOf("AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode") $sPostalCodeLowEnd = $oJson.StringOf("AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd") $sPostalCodeHighEnd = $oJson.StringOf("AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd") ConsoleWrite("Exact match!" & @CRLF) ConsoleWrite("postal code: " & $sPostalCodeLowEnd & @CRLF) Else ConsoleWrite("Non-Exact match." & @CRLF) $sCustomerContext = $oJson.StringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext") $statusCode = $oJson.StringOf("AddressValidationResponse.Response.ResponseStatusCode") $statusDescription = $oJson.StringOf("AddressValidationResponse.Response.ResponseStatusDescription") Local $i = 0 While $i < $iNumResults $oJson.I = $i $sRank = $oJson.StringOf("AddressValidationResponse.AddressValidationResult[i].Rank") ConsoleWrite("rank: " & $sRank & @CRLF) $sQuality = $oJson.StringOf("AddressValidationResponse.AddressValidationResult[i].Quality") $sAddressCity = $oJson.StringOf("AddressValidationResponse.AddressValidationResult[i].Address.City") ConsoleWrite("addressCity: " & $sAddressCity & @CRLF) $sAddressStateProvinceCode = $oJson.StringOf("AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode") $sPostalCodeLowEnd = $oJson.StringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd") ConsoleWrite("postal code: " & $sPostalCodeLowEnd & @CRLF) $sPostalCodeHighEnd = $oJson.StringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd") $i = $i + 1 Wend EndIf |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.