Sample code for 30+ languages & platforms
Rust

Shippo Validate Address

See more Shippo Examples

Demonstrates how to validate your addresses through the Shippo API before you create labels.

Chilkat Rust Downloads

Rust

// 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="215 Clayton St." \
//     -d city="San Francisco" \
//     -d state="CA" \
//     -d zip=94117 \
//     -d country="US" \
//     -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", "215 Clayton St.");
req.add_param("city", "San Francisco");
req.add_param("state", "CA");
req.add_param("zip", "94117");
req.add_param("country", "US");
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": "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

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 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 phone = j_resp.string_of("phone").unwrap_or_default();
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].code").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].text").unwrap_or_default();
    i = i + 1;
}