Rust
Rust
Example: Http.DownloadAppend method
Demonstrates the DownloadAppend method.Chilkat Rust Downloads
let target_path = "c:/temp/qa_output/download.txt".to_string();
let http = chilkat::Http::new();
http.set_keep_response_body(true);
// Assume the target file in the local filesystem does not yet exist.
let _ = http.download_append("https://chilkatsoft.com/testData/helloWorld.txt", &target_path).is_ok();
let mut status_code = http.last_status();
if status_code == 0 {
// Unable to either send the request or get the response.
println!("{}", http.last_error_text());
return;
}
// Should be 200.
println!("Response status code: {}", status_code);
// Examine the contents of the file.
let sb = chilkat::StringBuilder::new();
let _ = sb.load_file(&target_path, "utf-8");
println!("{}", sb.get_as_string().unwrap_or_default());
// Output:
// Response status code: 200
// Hello World!
// Download another text file and append to the target file.
let _ = http.download_append("https://chilkatsoft.com/testData/this_is_a_test.txt", &target_path).is_ok();
status_code = http.last_status();
if status_code == 0 {
// Unable to either send the request or get the response.
println!("{}", http.last_error_text());
return;
}
// Should be 200.
println!("Response status code: {}", status_code);
// Examine the contents of the file.
let _ = sb.load_file(&target_path, "utf-8");
println!("{}", sb.get_as_string().unwrap_or_default());
// Output:
// Response status code: 200
// Hello World!This is a Test.
// Delete the local target file.
let fac = chilkat::FileAccess::new();
let _ = fac.file_delete(&target_path);
// Try to download a file that does not exist:
let _ = http.download_append("https://chilkatsoft.com/testData/does_not_exist.txt", &target_path).is_ok();
status_code = http.last_status();
if status_code == 0 {
// Unable to either send the request or get the response.
println!("{}", http.last_error_text());
} else {
// We got a response, and we already know it wasn't a 200 success response.
// It should be a 404 not found.
println!("Response status code: {}", status_code);
// Examine the response body.
println!("Response body:");
println!("{}", http.last_response_body());
}