Rust
Rust
Download File from Dropbox into a String Variable
See more Dropbox Examples
Demonstrates how to download a file from Dropbox directly into a string variable.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// A Dropbox access token should have been previously obtained.
// Dropbox access tokens do not expire.
// See Dropbox Access Token.
let rest = chilkat::Rest::new();
// Connect to Dropbox
if rest.connect("content.dropboxapi.com", 443, true, true).is_err() {
println!("{}", rest.last_error_text());
return;
}
// Add request headers.
let _ = rest.add_header("Authorization", "Bearer DROPBOX_ACCESS_TOKEN");
// The download "parameters" are contained in JSON passed in an HTTP request header.
// This is the JSON indicating the file to be downloaded:
// {
// "path": "/jack.txt",
// }
let json = chilkat::JsonObject::new();
let _ = json.append_string("path", "/jack.txt");
let _ = rest.add_header("Dropbox-API-Arg", &json.emit().unwrap_or_default());
// The content of the file on Dropbox is returned.
let Ok(file_content) = rest.full_request_no_body("POST", "/2/files/download") else {
println!("{}", rest.last_error_text());
return;
};
// When successful, Dropbox responds with a 200 response code.
if rest.response_status_code() != 200 {
// Examine the request/response to see what happened.
println!("response status code = {}", rest.response_status_code());
println!("response status text = {}", rest.response_status_text());
println!("response header: {}", rest.response_header());
println!("response body (if any): {}", file_content);
println!("---");
println!("LastRequestStartLine: {}", rest.last_request_start_line());
println!("LastRequestHeader: {}", rest.last_request_header());
return;
}
// Show the file content that was downloaded:
println!("{}", file_content);
println!("----");
// Information about the downloaded file is also available as JSON in a response header.
// The "dropbox-api-result" response header contains the information. For example:
let api_result = rest.response_hdr_by_name("dropbox-api-result").unwrap_or_default();
println!("{}", api_result);
// In this case, the pretty-formatted dropbox-api-result JSON looks like this:
// {
// "name": "jack.txt",
// "path_lower": "/jack.txt",
// "path_display": "/jack.txt",
// "id": "id:yqx4-tE_NKAAAAAAAAAAAQ",
// "client_modified": "2016-06-02T20:42:11Z",
// "server_modified": "2016-06-02T20:42:11Z",
// "rev": "8482db15f",
// "size": 42
// }
// Load the JSON, pretty-print it, and demonstrate how to get some values...
let json_result = chilkat::JsonObject::new();
json_result.set_emit_compact(false);
let _ = json_result.load(&api_result);
// Show the JSON pretty-printed...
println!("{}", json_result.emit().unwrap_or_default());
// Sample code to get data from the JSON response:
let size = json_result.int_of("size");
println!("size = {}", size);
let rev = json_result.string_of("rev").unwrap_or_default();
println!("rev = {}", rev);
let client_modified = json_result.string_of("client_modified").unwrap_or_default();
let ckdt = chilkat::DateTime::new();
let _ = ckdt.set_from_timestamp(&client_modified);
let b_local_time = true;
let dt = chilkat::DtObj::new();
ckdt.to_dt_obj(b_local_time, &dt);
println!("{}/{}/{} {}:{}", dt.day(), dt.month(), dt.year(), dt.hour(), dt.minute());