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
(PowerBuilder) UPS Address Validation (City, State, Zip)Demonstrates making a call to the UPS address validation REST API.
integer li_rc integer li_Success oleobject loo_Http string ls_Url oleobject loo_Json oleobject loo_Sb oleobject loo_Resp string ls_CustomerContext string ls_StatusCode string ls_StatusDescription string ls_ResultRank string ls_ResultQuality string ls_City string ls_ProvinceCode string ls_PostalCodeLowEnd string ls_PostalCodeHighEnd string ls_Rank string ls_Quality string ls_AddressCity string ls_AddressStateProvinceCode integer li_NumResults integer i // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Http = create oleobject // Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0 li_rc = loo_Http.ConnectToNewObject("Chilkat.Http") if li_rc < 0 then destroy loo_Http MessageBox("Error","Connecting to COM object failed") return end if // This is the testing endpoint for address validation: ls_Url = "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. loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject") loo_Json.UpdateString("AccessRequest.AccessLicenseNumber","UPS_ACCESS_KEY") loo_Json.UpdateString("AccessRequest.UserId","UPS_USERNAME") loo_Json.UpdateString("AccessRequest.Password","UPS_PASSWORD") loo_Json.UpdateString("AddressValidationRequest.Request.TransactionReference.CustomerContext","Your Customer Context") loo_Json.UpdateString("AddressValidationRequest.Request.RequestAction","AV") loo_Json.UpdateString("AddressValidationRequest.Address.City","ALPHARETTA") // We're making an intentional mistake here by passing CA instead of GA. loo_Json.UpdateString("AddressValidationRequest.Address.StateProvinceCode","CA") loo_Json.UpdateString("AddressValidationRequest.Address.PostalCode","30005") loo_Sb = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder") loo_Resp = loo_Http.PostJson3(ls_Url,"application/json",loo_Json) if loo_Http.LastMethodSuccess <> 1 then Write-Debug loo_Http.LastErrorText destroy loo_Http destroy loo_Json destroy loo_Sb return end if Write-Debug "status = " + string(loo_Resp.StatusCode) // A 200 response status indicate success. if loo_Resp.StatusCode <> 200 then Write-Debug loo_Resp.BodyStr Write-Debug "Failed." destroy loo_Resp destroy loo_Http destroy loo_Json destroy loo_Sb return end if loo_Json.Load(loo_Resp.BodyStr) loo_Json.EmitCompact = 0 Write-Debug loo_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. li_NumResults = loo_Json.SizeOfArray("AddressValidationResponse.AddressValidationResult") if li_NumResults < 0 then // Here's parse code for the above JSON exact response: ls_CustomerContext = loo_Json.StringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext") ls_StatusCode = loo_Json.StringOf("AddressValidationResponse.Response.ResponseStatusCode") ls_StatusDescription = loo_Json.StringOf("AddressValidationResponse.Response.ResponseStatusDescription") ls_ResultRank = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult.Rank") ls_ResultQuality = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult.Quality") ls_City = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult.Address.City") ls_ProvinceCode = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode") ls_PostalCodeLowEnd = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd") ls_PostalCodeHighEnd = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd") Write-Debug "Exact match!" Write-Debug "postal code: " + ls_PostalCodeLowEnd else Write-Debug "Non-Exact match." ls_CustomerContext = loo_Json.StringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext") ls_StatusCode = loo_Json.StringOf("AddressValidationResponse.Response.ResponseStatusCode") ls_StatusDescription = loo_Json.StringOf("AddressValidationResponse.Response.ResponseStatusDescription") i = 0 do while i < li_NumResults loo_Json.I = i ls_Rank = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult[i].Rank") Write-Debug "rank: " + ls_Rank ls_Quality = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult[i].Quality") ls_AddressCity = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult[i].Address.City") Write-Debug "addressCity: " + ls_AddressCity ls_AddressStateProvinceCode = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode") ls_PostalCodeLowEnd = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd") Write-Debug "postal code: " + ls_PostalCodeLowEnd ls_PostalCodeHighEnd = loo_Json.StringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd") i = i + 1 loop end if destroy loo_Resp destroy loo_Http destroy loo_Json destroy loo_Sb |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.