Rust
Rust
Shippo Validate Global Address
See more Shippo Examples
Demonstrates how to validate a global address.Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = chilkat::Http::new();
// 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="Kortrijksesteenweg 1005" \
// -d city="Gent" \
// -d zip=9000 \
// -d country="BE" \
// -d email="shippotle@goshippo.com"\
// -d validate=true
let req = chilkat::HttpRequest::new();
req.set_http_verb("POST");
req.set_path("/addresses/");
req.set_content_type("application/x-www-form-urlencoded");
req.add_param("name", "Shawn Ippotle");
req.add_param("company", "Shippo");
req.add_param("street1", "Kortrijksesteenweg 1005");
req.add_param("city", "Gent");
req.add_param("zip", "9000");
req.add_param("country", "BE");
req.add_param("email", "shippotle@goshippo.com");
req.add_param("validate", "true");
req.add_header("Authorization", "ShippoToken <API_TOKEN>");
let resp = chilkat::HttpResponse::new();
if http.http_req("https://api.goshippo.com/addresses/", &req, &resp).is_err() {
println!("{}", http.last_error_text());
return;
}
let sb_response_body = chilkat::StringBuilder::new();
let _ = resp.get_body_sb(&sb_response_body);
let j_resp = chilkat::JsonObject::new();
let _ = j_resp.load_sb(&sb_response_body);
j_resp.set_emit_compact(false);
println!("Response Body:");
println!("{}", j_resp.emit().unwrap_or_default());
let resp_status_code = resp.status_code();
println!("Response Status Code = {}", resp_status_code);
if resp_status_code >= 400 {
println!("Response Header:");
println!("{}", resp.header());
println!("Failed.");
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "object_created": "2017-07-26T17:52:37.305Z",
// "object_updated": "2017-07-26T17:52:37.351Z",
// "object_id": "b7f9709df3914d1ca6efe4c30e7b0572",
// "is_complete": true,
// "validation_results": {
// "is_valid": true,
// "messages": [
// {
// "source": "Shippo Address Validator",
// "type": "address_correction",
// "code": "administrative_area_change",
// "text": "The administrative area (state or province) was added or changed."
// },
// {
// "source": "Shippo Address Validator",
// "code": "geocoded_rooftop",
// "text": "The record was geocoded down to rooftop level, meaning the point is within the property boundaries (most often the center)."
// },
// {
// "source": "Shippo Address Validator",
// "code": "premises_full",
// "text": "The address has been verified to the Premise (House or Building) Level, which is the highest level possible with the reference data."
// }
// ]
// },
// "object_owner": "hippo@goshippo.com",
// "name": "Hippo Shippo",
// "company": "Shippo",
// "street_no": "2",
// "street1": "Unter den Linden",
// "street2": "",
// "street3": "",
// "city": "Berlin",
// "state": "",
// "zip": "10117",
// "country": "DE",
// "longitude": "13.39751",
// "latitude": "52.51785",
// "phone": "14151234567",
// "email": "hippo@goshippo.com",
// "is_residential": null,
// "metadata": "",
// "test": false
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
let object_created = j_resp.string_of("object_created").unwrap_or_default();
let object_updated = j_resp.string_of("object_updated").unwrap_or_default();
let object_id = j_resp.string_of("object_id").unwrap_or_default();
let is_complete = j_resp.bool_of("is_complete");
let validation_results_is_valid = j_resp.bool_of("validation_results.is_valid");
let object_owner = j_resp.string_of("object_owner").unwrap_or_default();
let name = j_resp.string_of("name").unwrap_or_default();
let company = j_resp.string_of("company").unwrap_or_default();
let street_no = j_resp.string_of("street_no").unwrap_or_default();
let street1 = j_resp.string_of("street1").unwrap_or_default();
let street2 = j_resp.string_of("street2").unwrap_or_default();
let street3 = j_resp.string_of("street3").unwrap_or_default();
let city = j_resp.string_of("city").unwrap_or_default();
let state = j_resp.string_of("state").unwrap_or_default();
let zip = j_resp.string_of("zip").unwrap_or_default();
let country = j_resp.string_of("country").unwrap_or_default();
let longitude = j_resp.string_of("longitude").unwrap_or_default();
let latitude = j_resp.string_of("latitude").unwrap_or_default();
let phone = j_resp.string_of("phone").unwrap_or_default();
let email = j_resp.string_of("email").unwrap_or_default();
let is_residential = j_resp.string_of("is_residential").unwrap_or_default();
let metadata = j_resp.string_of("metadata").unwrap_or_default();
let test = j_resp.bool_of("test");
let mut i = 0;
let count_i = j_resp.size_of_array("validation_results.messages");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.string_of("validation_results.messages[i].source").unwrap_or_default();
let _ = j_resp.string_of("validation_results.messages[i].type").unwrap_or_default();
let _ = j_resp.string_of("validation_results.messages[i].code").unwrap_or_default();
let _ = j_resp.string_of("validation_results.messages[i].text").unwrap_or_default();
i = i + 1;
}