Rust
Rust
Shippo Create Shipment Object
See more Shippo Examples
Create a Shipment object to retrieve rates. It represents a request to ship a given package from the sender to the recipient 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/shipments/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -H "Content-Type: application/json" \
// -d '{
// "address_from":{
// "name":"Mr. Hippo",
// "street1":"215 Clayton St.",
// "city":"San Francisco",
// "state":"CA",
// "zip":"94117",
// "country":"US"
// },
// "address_to":{
// "name":"Mrs. Hippo",
// "street1":"965 Mission St.",
// "city":"San Francisco",
// "state":"CA",
// "zip":"94105",
// "country":"US"
// },
// "parcels":[{
// "length":"5",
// "width":"5",
// "height":"5",
// "distance_unit":"in",
// "weight":"2",
// "mass_unit":"lb"
// }],
// "async": false
// }'
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "address_from": {
// "name": "Mr. Hippo",
// "street1": "215 Clayton St.",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US"
// },
// "address_to": {
// "name": "Mrs. Hippo",
// "street1": "965 Mission St.",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94105",
// "country": "US"
// },
// "parcels": [
// {
// "length": "5",
// "width": "5",
// "height": "5",
// "distance_unit": "in",
// "weight": "2",
// "mass_unit": "lb"
// }
// ],
// "async": false
// }
let json = chilkat::JsonObject::new();
let _ = json.update_string("address_from.name", "Mr. Hippo");
let _ = json.update_string("address_from.street1", "215 Clayton St.");
let _ = json.update_string("address_from.city", "San Francisco");
let _ = json.update_string("address_from.state", "CA");
let _ = json.update_string("address_from.zip", "94117");
let _ = json.update_string("address_from.country", "US");
let _ = json.update_string("address_to.name", "Mrs. Hippo");
let _ = json.update_string("address_to.street1", "965 Mission St.");
let _ = json.update_string("address_to.city", "San Francisco");
let _ = json.update_string("address_to.state", "CA");
let _ = json.update_string("address_to.zip", "94105");
let _ = json.update_string("address_to.country", "US");
let _ = json.update_string("parcels[0].length", "5");
let _ = json.update_string("parcels[0].width", "5");
let _ = json.update_string("parcels[0].height", "5");
let _ = json.update_string("parcels[0].distance_unit", "in");
let _ = json.update_string("parcels[0].weight", "2");
let _ = json.update_string("parcels[0].mass_unit", "lb");
let _ = json.update_bool("async", false);
http.set_request_header("Authorization", "ShippoToken <API_TOKEN>");
http.set_request_header("Content-Type", "application/json");
let resp = chilkat::HttpResponse::new();
if http.http_json("POST", "https://api.goshippo.com/shipments/", &json, "application/json", &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)
// {
// "carrier_accounts": [
// ],
// "object_created": "2019-06-28T17:45:20.441Z",
// "object_updated": "2019-06-28T17:45:20.511Z",
// "object_id": "17b01795a4884d8a9002ccc17c9d09d1",
// "object_owner": "admin@chilkatsoft.com",
// "status": "SUCCESS",
// "address_from": {
// "object_id": "427489906b604f498f7bd2429ab7d9a1",
// "is_complete": true,
// "name": "Mr. Hippo",
// "company": "",
// "street_no": "",
// "street1": "215 Clayton St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "address_to": {
// "object_id": "3d7166269a2844b7842ef9ace942ec7c",
// "is_complete": true,
// "name": "Mrs. Hippo",
// "company": "",
// "street_no": "",
// "street1": "965 Mission St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94105",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "parcels": [
// {
// "object_state": "VALID",
// "object_created": "2019-06-28T17:45:20.411Z",
// "object_updated": "2019-06-28T17:45:20.455Z",
// "object_id": "5af766ff15684a4186b0e3c833348fac",
// "object_owner": "admin@chilkatsoft.com",
// "template": null,
// "extra": {},
// "length": "5.0000",
// "width": "5.0000",
// "height": "5.0000",
// "distance_unit": "in",
// "weight": "2.0000",
// "mass_unit": "lb",
// "value_amount": null,
// "value_currency": null,
// "metadata": "",
// "line_items": [
// ],
// "test": true
// }
// ],
// "shipment_date": "2019-06-28T17:45:20.511Z",
// "address_return": {
// "object_id": "427489906b604f498f7bd2429ab7d9a1",
// "is_complete": true,
// "name": "Mr. Hippo",
// "company": "",
// "street_no": "",
// "street1": "215 Clayton St.",
// "validation_results": {},
// "street2": "",
// "street3": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "",
// "email": "",
// "is_residential": null,
// "test": true
// },
// "alternate_address_to": null,
// "customs_declaration": null,
// "extra": {},
// "rates": [
// {
// "object_created": "2019-06-28T17:45:20.789Z",
// "object_id": "dd7556c284e8444294d2ab7099e662d2",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// "FASTEST"
// ],
// "amount": "22.78",
// "currency": "USD",
// "amount_local": "22.78",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Priority Mail Express",
// "token": "usps_priority_express",
// "terms": ""
// },
// "estimated_days": 2,
// "arrives_by": null,
// "duration_terms": "Overnight delivery to most U.S. locations.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// },
// {
// "object_created": "2019-06-28T17:45:20.786Z",
// "object_id": "ec11cc297e2f4583986097f6d409e5c6",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// "BESTVALUE",
// "CHEAPEST",
// "FASTEST"
// ],
// "amount": "6.95",
// "currency": "USD",
// "amount_local": "6.95",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Priority Mail",
// "token": "usps_priority",
// "terms": ""
// },
// "estimated_days": 2,
// "arrives_by": null,
// "duration_terms": "Delivery within 1, 2, or 3 days based on where your package started and where it’s being sent.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// },
// {
// "object_created": "2019-06-28T17:45:20.785Z",
// "object_id": "d256586f539f4c93a2f02cc11f4942ac",
// "object_owner": "admin@chilkatsoft.com",
// "shipment": "17b01795a4884d8a9002ccc17c9d09d1",
// "attributes": [
// ],
// "amount": "7.32",
// "currency": "USD",
// "amount_local": "7.32",
// "currency_local": "USD",
// "provider": "USPS",
// "provider_image_75": "https://shippo-static.s3.amazonaws.com/providers/75/USPS.png",
// "provider_image_200": "https://shippo-static.s3.amazonaws.com/providers/200/USPS.png",
// "servicelevel": {
// "name": "Parcel Select",
// "token": "usps_parcel_select",
// "terms": ""
// },
// "estimated_days": 7,
// "arrives_by": null,
// "duration_terms": "Delivery in 2 to 8 days.",
// "messages": [
// ],
// "carrier_account": "1f3dc340ad6c4991ad61f14fa3e4b82e",
// "test": true,
// "zone": "1"
// }
// ],
// "messages": [
// {
// "source": "DHLExpress",
// "code": "",
// "text": "Shippo's DHL Express master account doesn't support shipments to inside of the US"
// }
// ],
// "metadata": "",
// "test": true,
// "order": null
// }
// 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 mut j: i32 = 0;
let mut count_j: i32 = 0;
let mut object_created = j_resp.string_of("object_created").unwrap_or_default();
let mut object_updated = j_resp.string_of("object_updated").unwrap_or_default();
let mut object_id = j_resp.string_of("object_id").unwrap_or_default();
let mut object_owner = j_resp.string_of("object_owner").unwrap_or_default();
let status = j_resp.string_of("status").unwrap_or_default();
let address_from_object_id = j_resp.string_of("address_from.object_id").unwrap_or_default();
let address_from_is_complete = j_resp.bool_of("address_from.is_complete");
let address_from_name = j_resp.string_of("address_from.name").unwrap_or_default();
let address_from_company = j_resp.string_of("address_from.company").unwrap_or_default();
let address_from_street_no = j_resp.string_of("address_from.street_no").unwrap_or_default();
let address_from_street1 = j_resp.string_of("address_from.street1").unwrap_or_default();
let address_from_street2 = j_resp.string_of("address_from.street2").unwrap_or_default();
let address_from_street3 = j_resp.string_of("address_from.street3").unwrap_or_default();
let address_from_city = j_resp.string_of("address_from.city").unwrap_or_default();
let address_from_state = j_resp.string_of("address_from.state").unwrap_or_default();
let address_from_zip = j_resp.string_of("address_from.zip").unwrap_or_default();
let address_from_country = j_resp.string_of("address_from.country").unwrap_or_default();
let address_from_phone = j_resp.string_of("address_from.phone").unwrap_or_default();
let address_from_email = j_resp.string_of("address_from.email").unwrap_or_default();
let address_from_is_residential = j_resp.string_of("address_from.is_residential").unwrap_or_default();
let address_from_test = j_resp.bool_of("address_from.test");
let address_to_object_id = j_resp.string_of("address_to.object_id").unwrap_or_default();
let address_to_is_complete = j_resp.bool_of("address_to.is_complete");
let address_to_name = j_resp.string_of("address_to.name").unwrap_or_default();
let address_to_company = j_resp.string_of("address_to.company").unwrap_or_default();
let address_to_street_no = j_resp.string_of("address_to.street_no").unwrap_or_default();
let address_to_street1 = j_resp.string_of("address_to.street1").unwrap_or_default();
let address_to_street2 = j_resp.string_of("address_to.street2").unwrap_or_default();
let address_to_street3 = j_resp.string_of("address_to.street3").unwrap_or_default();
let address_to_city = j_resp.string_of("address_to.city").unwrap_or_default();
let address_to_state = j_resp.string_of("address_to.state").unwrap_or_default();
let address_to_zip = j_resp.string_of("address_to.zip").unwrap_or_default();
let address_to_country = j_resp.string_of("address_to.country").unwrap_or_default();
let address_to_phone = j_resp.string_of("address_to.phone").unwrap_or_default();
let address_to_email = j_resp.string_of("address_to.email").unwrap_or_default();
let address_to_is_residential = j_resp.string_of("address_to.is_residential").unwrap_or_default();
let address_to_test = j_resp.bool_of("address_to.test");
let shipment_date = j_resp.string_of("shipment_date").unwrap_or_default();
let address_return_object_id = j_resp.string_of("address_return.object_id").unwrap_or_default();
let address_return_is_complete = j_resp.bool_of("address_return.is_complete");
let address_return_name = j_resp.string_of("address_return.name").unwrap_or_default();
let address_return_company = j_resp.string_of("address_return.company").unwrap_or_default();
let address_return_street_no = j_resp.string_of("address_return.street_no").unwrap_or_default();
let address_return_street1 = j_resp.string_of("address_return.street1").unwrap_or_default();
let address_return_street2 = j_resp.string_of("address_return.street2").unwrap_or_default();
let address_return_street3 = j_resp.string_of("address_return.street3").unwrap_or_default();
let address_return_city = j_resp.string_of("address_return.city").unwrap_or_default();
let address_return_state = j_resp.string_of("address_return.state").unwrap_or_default();
let address_return_zip = j_resp.string_of("address_return.zip").unwrap_or_default();
let address_return_country = j_resp.string_of("address_return.country").unwrap_or_default();
let address_return_phone = j_resp.string_of("address_return.phone").unwrap_or_default();
let address_return_email = j_resp.string_of("address_return.email").unwrap_or_default();
let address_return_is_residential = j_resp.string_of("address_return.is_residential").unwrap_or_default();
let address_return_test = j_resp.bool_of("address_return.test");
let alternate_address_to = j_resp.string_of("alternate_address_to").unwrap_or_default();
let customs_declaration = j_resp.string_of("customs_declaration").unwrap_or_default();
let mut metadata = j_resp.string_of("metadata").unwrap_or_default();
let mut test = j_resp.bool_of("test");
let order = j_resp.string_of("order").unwrap_or_default();
let mut i = 0;
let mut count_i = j_resp.size_of_array("carrier_accounts");
while i < count_i {
j_resp.set_i(i);
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("parcels");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.string_of("parcels[i].object_state").unwrap_or_default();
object_created = j_resp.string_of("parcels[i].object_created").unwrap_or_default();
object_updated = j_resp.string_of("parcels[i].object_updated").unwrap_or_default();
object_id = j_resp.string_of("parcels[i].object_id").unwrap_or_default();
object_owner = j_resp.string_of("parcels[i].object_owner").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].template").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].length").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].width").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].height").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].distance_unit").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].weight").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].mass_unit").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].value_amount").unwrap_or_default();
let _ = j_resp.string_of("parcels[i].value_currency").unwrap_or_default();
metadata = j_resp.string_of("parcels[i].metadata").unwrap_or_default();
test = j_resp.bool_of("parcels[i].test");
j = 0;
count_j = j_resp.size_of_array("parcels[i].line_items");
while j < count_j {
j_resp.set_j(j);
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("rates");
while i < count_i {
j_resp.set_i(i);
object_created = j_resp.string_of("rates[i].object_created").unwrap_or_default();
object_id = j_resp.string_of("rates[i].object_id").unwrap_or_default();
object_owner = j_resp.string_of("rates[i].object_owner").unwrap_or_default();
let _ = j_resp.string_of("rates[i].shipment").unwrap_or_default();
let _ = j_resp.string_of("rates[i].amount").unwrap_or_default();
let _ = j_resp.string_of("rates[i].currency").unwrap_or_default();
let _ = j_resp.string_of("rates[i].amount_local").unwrap_or_default();
let _ = j_resp.string_of("rates[i].currency_local").unwrap_or_default();
let _ = j_resp.string_of("rates[i].provider").unwrap_or_default();
let _ = j_resp.string_of("rates[i].provider_image_75").unwrap_or_default();
let _ = j_resp.string_of("rates[i].provider_image_200").unwrap_or_default();
let _ = j_resp.string_of("rates[i].servicelevel.name").unwrap_or_default();
let _ = j_resp.string_of("rates[i].servicelevel.token").unwrap_or_default();
let _ = j_resp.string_of("rates[i].servicelevel.terms").unwrap_or_default();
let _ = j_resp.int_of("rates[i].estimated_days");
let _ = j_resp.string_of("rates[i].arrives_by").unwrap_or_default();
let _ = j_resp.string_of("rates[i].duration_terms").unwrap_or_default();
let _ = j_resp.string_of("rates[i].carrier_account").unwrap_or_default();
test = j_resp.bool_of("rates[i].test");
let _ = j_resp.string_of("rates[i].zone").unwrap_or_default();
j = 0;
count_j = j_resp.size_of_array("rates[i].attributes");
while j < count_j {
j_resp.set_j(j);
let _ = j_resp.string_of("rates[i].attributes[j]").unwrap_or_default();
j = j + 1;
}
j = 0;
count_j = j_resp.size_of_array("rates[i].messages");
while j < count_j {
j_resp.set_j(j);
j = j + 1;
}
i = i + 1;
}
i = 0;
count_i = j_resp.size_of_array("messages");
while i < count_i {
j_resp.set_i(i);
let _ = j_resp.string_of("messages[i].source").unwrap_or_default();
let _ = j_resp.string_of("messages[i].code").unwrap_or_default();
let _ = j_resp.string_of("messages[i].text").unwrap_or_default();
i = i + 1;
}