Sample code for 30+ languages & platforms
Rust

Upload String to Dropbox

See more Dropbox Examples

Uploads a string to a file on Dropbox. This example can upload content up to 150MB, assuming a 150MB string fits in memory for your app. Larger files must be uploaded with an upload session (upload_session/start).

Chilkat Rust Downloads

Rust

// 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("Content-Type", "application/octet-stream");
let _ = rest.add_header("Authorization", "Bearer DROPBOX_ACCESS_TOKEN");

// The upload "parameters" contained in JSON passed in an HTTP request header.
// This is the JSON to be added in this example:
// { 
//    "path": "/jack.txt",
//    "mode": "add",
//    "autorename": true,
//    "mute": false
// }

// This will be the content of the file created in Dropbox.
let content = "All work and no play makes Jack a dull boy".to_string();

let json = chilkat::JsonObject::new();
let _ = json.append_string("path", "/jack.txt");
let _ = json.append_string("mode", "add");
let _ = json.append_bool("autorename", true);
let _ = json.append_bool("mute", false);
let _ = rest.add_header("Dropbox-API-Arg", &json.emit().unwrap_or_default());

// Do the upload.  The URL is https://content.dropboxapi.com/2/files/upload.
// We already connected to content.dropboxapi.com using TLS (i.e. HTTPS),
// so now we only need to specify the path "/2/files/upload".

let Ok(response_str) = rest.full_request_string("POST", "/2/files/upload", &content) 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): {}", response_str);
    println!("---");
    println!("LastRequestStartLine: {}", rest.last_request_start_line());
    println!("LastRequestHeader: {}", rest.last_request_header());
    return;
}

println!("LastRequestHeader: {}", rest.last_request_header());

// The response is JSON.
let json_resp = chilkat::JsonObject::new();
json_resp.set_emit_compact(false);
let _ = json_resp.load(&response_str);

// Show the JSON response.
println!("{}", json_resp.emit().unwrap_or_default());

// Returns JSON that 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
// }

// Sample code to get data from the JSON response:
let size = json_resp.int_of("size");
println!("size = {}", size);

let rev = json_resp.string_of("rev").unwrap_or_default();
println!("rev = {}", rev);

let client_modified = json_resp.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());