Xbase++
Xbase++
Shippo Validate Address
See more Shippo Examples
Demonstrates how to validate your addresses through the Shippo API before you create labels.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oReq
LOCAL oResp
LOCAL oSbResponseBody
LOCAL oJResp
LOCAL nRespStatusCode
LOCAL cSource
LOCAL cCode
LOCAL cV_type
LOCAL cText
LOCAL cObject_created
LOCAL cObject_updated
LOCAL cObject_id
LOCAL nIs_complete
LOCAL nValidation_resultsIs_valid
LOCAL cObject_owner
LOCAL cName
LOCAL cCompany
LOCAL cStreet_no
LOCAL cStreet1
LOCAL cStreet2
LOCAL cCity
LOCAL cState
LOCAL cZip
LOCAL cCountry
LOCAL cPhone
LOCAL i
LOCAL nCount_i
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oHttp := CreateObject("Chilkat.Http")
// Implements the following CURL command:
// curl https://api.goshippo.com/addresses/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -d name="Shawn Ippotle" \
// -d company="Shippo" \
// -d street1="215 Clayton St." \
// -d city="San Francisco" \
// -d state="CA" \
// -d zip=94117 \
// -d country="US" \
// -d email="shippotle@goshippo.com"\
// -d validate=true
oReq := CreateObject("Chilkat.HttpRequest")
oReq:HttpVerb := "POST"
oReq:Path := "/addresses/"
oReq:ContentType := "application/x-www-form-urlencoded"
oReq:AddParam("name", "Shawn Ippotle")
oReq:AddParam("company", "Shippo")
oReq:AddParam("street1", "215 Clayton St.")
oReq:AddParam("city", "San Francisco")
oReq:AddParam("state", "CA")
oReq:AddParam("zip", "94117")
oReq:AddParam("country", "US")
oReq:AddParam("email", "shippotle@goshippo.com")
oReq:AddParam("validate", "true")
oReq:AddHeader("Authorization", "ShippoToken <API_TOKEN>")
oResp := CreateObject("Chilkat.HttpResponse")
nSuccess := oHttp:HttpReq("https://api.goshippo.com/addresses/", oReq, oResp)
IF (nSuccess == 0)
? oHttp:LastErrorText
oHttp:destroy()
oReq:destroy()
oResp:destroy()
RETURN
ENDIF
oSbResponseBody := CreateObject("Chilkat.StringBuilder")
oResp:GetBodySb(oSbResponseBody)
oJResp := CreateObject("Chilkat.JsonObject")
oJResp:LoadSb(oSbResponseBody)
oJResp:EmitCompact := 0
? "Response Body:"
? oJResp:Emit()
nRespStatusCode := oResp:StatusCode
? "Response Status Code = " + Str(nRespStatusCode)
IF (nRespStatusCode >= 400)
? "Response Header:"
? oResp:Header
? "Failed."
oHttp:destroy()
oReq:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()
RETURN
ENDIF
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "object_created": "2015-03-30T23:47:11.574Z",
// "object_updated": "2015-03-30T23:47:11.596Z",
// "object_id": "67183b2e81e9421f894bfbcdc4236b16",
// "is_complete": false,
// "validation_results": {
// "is_valid": false,
// "messages": [
// {
// "source": "USPS",
// "code": "Address Not Found",
// "type": "address_error",
// "text": "The address as submitted could not be found. Please check for excessive abbreviations in the street address line or in the City name."
// }
// ]
// },
// "object_owner": "shippotle@goshippo.com",
// "name": "Shawn Ippotle",
// "company": "Shippo",
// "street_no": "",
// "street1": "215 HIPPO ST.",
// "street2": "",
// "city": "SAN FRANCISCO",
// "state": "CA",
// "zip": "94107",
// "country": "US",
// "phone": ""
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
cObject_created := oJResp:StringOf("object_created")
cObject_updated := oJResp:StringOf("object_updated")
cObject_id := oJResp:StringOf("object_id")
nIs_complete := oJResp:BoolOf("is_complete")
nValidation_resultsIs_valid := oJResp:BoolOf("validation_results.is_valid")
cObject_owner := oJResp:StringOf("object_owner")
cName := oJResp:StringOf("name")
cCompany := oJResp:StringOf("company")
cStreet_no := oJResp:StringOf("street_no")
cStreet1 := oJResp:StringOf("street1")
cStreet2 := oJResp:StringOf("street2")
cCity := oJResp:StringOf("city")
cState := oJResp:StringOf("state")
cZip := oJResp:StringOf("zip")
cCountry := oJResp:StringOf("country")
cPhone := oJResp:StringOf("phone")
i := 0
nCount_i := oJResp:SizeOfArray("validation_results.messages")
DO WHILE i < nCount_i
oJResp:I := i
cSource := oJResp:StringOf("validation_results.messages[i].source")
cCode := oJResp:StringOf("validation_results.messages[i].code")
cV_type := oJResp:StringOf("validation_results.messages[i].type")
cText := oJResp:StringOf("validation_results.messages[i].text")
i := i + 1
ENDDO
oHttp:destroy()
oReq:destroy()
oResp:destroy()
oSbResponseBody:destroy()
oJResp:destroy()