Rust
Rust
Send a REST Request with a Binary Body
See more REST Examples
Demonstrates Rest.FullRequestBd, which sends a complete request whose binary body is read from a BinData and stores the text response body in a StringBuilder.
The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background. FullRequestBd is used when the request payload is binary, such as an image or other file, while the response is textual (for example a JSON acknowledgement).
Chilkat Rust Downloads
let rest = chilkat::Rest::new();
let b_tls = true;
let b_auto_reconnect = true;
if rest.connect("example.com", 443, b_tls, b_auto_reconnect).is_err() {
println!("{}", rest.last_error_text());
return;
}
// The file paths are relative to the application's current working directory. Absolute paths
// may also be used. Supply the paths appropriate to your own environment.
// FullRequestBd sends a request whose binary body is read from a BinData and stores the text
// response body in a StringBuilder.
let bd_request = chilkat::BinData::new();
if bd_request.load_file("qa_data/image.png").is_err() {
println!("Failed to load the request body file.");
return;
}
let _ = rest.add_header("Content-Type", "image/png");
let sb_response = chilkat::StringBuilder::new();
if rest.full_request_bd("PUT", "/api/images/1", &bd_request, &sb_response).is_err() {
println!("{}", rest.last_error_text());
return;
}
let Ok(response_text) = sb_response.get_as_string() else {
println!("{}", sb_response.last_error_text());
return;
};
println!("{}", response_text);