Rust Requires Chilkat v11.0.0+
Rust
Inspect the MIME parts of a Gmail message
See more GMail REST API Examples
Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.
Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Rust Downloads
let mut success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let token = chilkat::JsonObject::new();
success = token.load_file("gmail-token.json").is_ok();
if !success {
println!("Could not read gmail-token.json from the private working directory.");
return;
}
if (token.json_type_of("access_token") != 1) || (token.string_of_equals("access_token", "", true)) {
println!("An OAuth access token is required.");
return;
}
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_auth_token(&token.string_of("access_token").unwrap_or_default());
let resp = chilkat::HttpResponse::new();
let reply = chilkat::JsonObject::new();
let url = chilkat::StringBuilder::new();
let _ = url.append("https://gmail.googleapis.com/gmail/v1/users/me/messages/");
let _ = url.append("GMAIL_MESSAGE_ID");
let _ = url.append("?format=full");
success = http.http_no_body("GET", &url.get_as_string().unwrap_or_default(), &resp).is_ok();
if resp.status_code() == 0 {
println!("Gmail did not return an HTTP response.");
return;
}
if (!success) || ((resp.status_code() < 200) || (resp.status_code() >= 300)) {
println!("Gmail HTTP {}", resp.status_code());
if reply.load(&resp.body_str()).is_ok() {
println!("{}", reply.string_of("error.message").unwrap_or_default());
}
return;
}
if reply.load(&resp.body_str()).is_err() {
println!("Expected a Gmail JSON response.");
return;
}
if reply.json_type_of("payload") != 3 {
println!("Expected a MIME payload.");
return;
}
// Queue paths into the original JSON document so any MIME nesting depth is supported.
let paths = chilkat::StringTable::new();
if paths.append("payload").is_err() {
println!("Could not queue the root part.");
return;
}
let part = chilkat::JsonObject::new();
let child_path = chilkat::StringBuilder::new();
let mut p = 0;
while p < paths.count() {
if reply.object_of2(&paths.string_at(p).unwrap_or_default(), &part).is_err() {
println!("Could not read a MIME part.");
return;
}
println!("Part: {} bytes: {}", part.string_of("mimeType").unwrap_or_default(), part.int_of("body.size"));
if part.json_type_of("filename") == 1 {
if !part.string_of_equals("filename", "", true) {
println!("Filename: {}", part.string_of("filename").unwrap_or_default());
}
}
if part.json_type_of("body.attachmentId") == 1 {
println!("Attachment ID: {}", part.string_of("body.attachmentId").unwrap_or_default());
} else {
if part.json_type_of("body.data") == 1 {
println!("Body data is available in this part.");
}
}
let mut j = 0;
while j < part.size_of_array("parts") {
let _ = child_path.set_string(&paths.string_at(p).unwrap_or_default());
let _ = child_path.append(".parts[");
let _ = child_path.append_int(j);
let _ = child_path.append("]");
if paths.append(&child_path.get_as_string().unwrap_or_default()).is_err() {
println!("Could not queue a child part.");
return;
}
j = j + 1;
}
p = p + 1;
}