Go
Go
UPS Address Validation (City, State, Zip)
See more HTTP Misc Examples
Demonstrates making a call to the UPS address validation REST API.Chilkat Go Downloads
success := false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := chilkat.NewHttp()
// This is the testing endpoint for address validation:
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.
json := chilkat.NewJsonObject()
json.UpdateString("AccessRequest.AccessLicenseNumber","UPS_ACCESS_KEY")
json.UpdateString("AccessRequest.UserId","UPS_USERNAME")
json.UpdateString("AccessRequest.Password","UPS_PASSWORD")
json.UpdateString("AddressValidationRequest.Request.TransactionReference.CustomerContext","Your Customer Context")
json.UpdateString("AddressValidationRequest.Request.RequestAction","AV")
json.UpdateString("AddressValidationRequest.Address.City","ALPHARETTA")
// We're making an intentional mistake here by passing CA instead of GA.
json.UpdateString("AddressValidationRequest.Address.StateProvinceCode","CA")
json.UpdateString("AddressValidationRequest.Address.PostalCode","30005")
sb := chilkat.NewStringBuilder()
resp := chilkat.NewHttpResponse()
success = http.HttpJson("POST",url,json,"application/json",resp)
if success == false {
fmt.Println(http.LastErrorText())
http.DisposeHttp()
json.DisposeJsonObject()
sb.DisposeStringBuilder()
resp.DisposeHttpResponse()
return
}
fmt.Println("status = ", resp.StatusCode())
// A 200 response status indicate success.
if resp.StatusCode() != 200 {
fmt.Println(resp.BodyStr())
fmt.Println("Failed.")
http.DisposeHttp()
json.DisposeJsonObject()
sb.DisposeStringBuilder()
resp.DisposeHttpResponse()
return
}
json.Load(resp.BodyStr())
json.SetEmitCompact(false)
fmt.Println(*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.
var customerContext *string = new(string)
var statusCode *string = new(string)
var statusDescription *string = new(string)
var resultRank *string = new(string)
var resultQuality *string = new(string)
var city *string = new(string)
var provinceCode *string = new(string)
var postalCodeLowEnd *string = new(string)
var postalCodeHighEnd *string = new(string)
var rank *string = new(string)
var quality *string = new(string)
var addressCity *string = new(string)
var addressStateProvinceCode *string = new(string)
numResults := json.SizeOfArray("AddressValidationResponse.AddressValidationResult")
if numResults < 0 {
// Here's parse code for the above JSON exact response:
customerContext = json.StringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext")
statusCode = json.StringOf("AddressValidationResponse.Response.ResponseStatusCode")
statusDescription = json.StringOf("AddressValidationResponse.Response.ResponseStatusDescription")
resultRank = json.StringOf("AddressValidationResponse.AddressValidationResult.Rank")
resultQuality = json.StringOf("AddressValidationResponse.AddressValidationResult.Quality")
city = json.StringOf("AddressValidationResponse.AddressValidationResult.Address.City")
provinceCode = json.StringOf("AddressValidationResponse.AddressValidationResult.Address.StateProvinceCode")
postalCodeLowEnd = json.StringOf("AddressValidationResponse.AddressValidationResult.PostalCodeLowEnd")
postalCodeHighEnd = json.StringOf("AddressValidationResponse.AddressValidationResult.PostalCodeHighEnd")
fmt.Println("Exact match!")
fmt.Println("postal code: ", *postalCodeLowEnd)
} else {
fmt.Println("Non-Exact match.")
customerContext = json.StringOf("AddressValidationResponse.Response.TransactionReference.CustomerContext")
statusCode = json.StringOf("AddressValidationResponse.Response.ResponseStatusCode")
statusDescription = json.StringOf("AddressValidationResponse.Response.ResponseStatusDescription")
i := 0
for i < numResults {
json.SetI(i)
rank = json.StringOf("AddressValidationResponse.AddressValidationResult[i].Rank")
fmt.Println("rank: ", *rank)
quality = json.StringOf("AddressValidationResponse.AddressValidationResult[i].Quality")
addressCity = json.StringOf("AddressValidationResponse.AddressValidationResult[i].Address.City")
fmt.Println("addressCity: ", *addressCity)
addressStateProvinceCode = json.StringOf("AddressValidationResponse.AddressValidationResult[i].Address.StateProvinceCode")
postalCodeLowEnd = json.StringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeLowEnd")
fmt.Println("postal code: ", *postalCodeLowEnd)
postalCodeHighEnd = json.StringOf("AddressValidationResponse.AddressValidationResult[i].PostalCodeHighEnd")
i = i + 1
}
}
http.DisposeHttp()
json.DisposeJsonObject()
sb.DisposeStringBuilder()
resp.DisposeHttpResponse()