Sample code for 30+ languages & platforms
Rust

REST Download Binary File to Memory

See more REST Examples

Download a binary file to a Chilkat BinData object.

Chilkat Rust Downloads

Rust

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

let rest = chilkat::Rest::new();

// We're going to download a sample MS-Word doc file.
// The URLs of our MS-Word sample documents are:

// https://www.chilkatdownload.com/sample_data/sample.doc
// https://www.chilkatdownload.com/sample_data/sample.docx

let path_part_of_url = "/sample_data/sample.doc".to_string();
let domain = "chilkatdownload.com".to_string();

let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
if rest.connect(&domain, port, b_tls, b_auto_reconnect).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

let bd = chilkat::BinData::new();
if rest.full_request_no_body_bd("GET", &path_part_of_url, &bd).is_err() {
    println!("{}", rest.last_error_text());
    return;
}

// A 200 response is expected for actual success.
// If we don't get a 200 response, then the response body was not actually
// the file data, but it was text containing error information.
if rest.response_status_code() != 200 {
    let sb_error_text = chilkat::StringBuilder::new();
    let _ = sb_error_text.append_bd(&bd, "utf-8", 0, 0);
    println!("{}", sb_error_text.get_as_string().unwrap_or_default());
    println!("-- Failed.");
    return;
}

// Save to a local file.
// Change the file path based on your operating system or needs...
if bd.write_file("c:/temp/qa_output/sample.doc").is_err() {
    println!("Failed to save to local file.");
    return;
}

println!("REST Download of MS-Word File was successful.");