Rust
Rust
REST Stream Multipart Body from File
See more REST Examples
Demonstrates how to send a multipart/form-data HTTP request, where one of the parts contains data streamed directly from a file. This is good for cases where the file to be uploaded is very large.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rest = chilkat::Rest::new();
// Connect to the destination web server.
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("www.somewebserver.com", port, b_tls, b_auto_reconnect).is_ok();
// This example will send the following multipart/form-data request.
// The Content-Length is automatically computed and added by Chilkat.
// POST /some_path HTTP/1.1
// Content-Type: multipart/form-data; boundary=---------------------------735323031399963166993862150
// Content-Length: 834
//
// -----------------------------735323031399963166993862150
// Content-Disposition: form-data; name="text1"
//
// text 123 abc
// -----------------------------735323031399963166993862150
// Content-Disposition: form-data; name="text2"
//
// xyz
// -----------------------------735323031399963166993862150
// Content-Disposition: form-data; name="file1"; filename="a.txt"
// Content-Type: text/plain
//
// Content of a.txt.
//
// -----------------------------735323031399963166993862150
// Content-Disposition: form-data; name="file2"; filename="a.html"
// Content-Type: text/html
//
// <!DOCTYPE html><title>Content of a.html.</title>
//
// -----------------------------735323031399963166993862150
// Content-Disposition: form-data; name="file3"; filename="starfish.jpg"
// Content-Type: image/jpeg
//
// binary data goes here
// -----------------------------735323031399963166993862150--
// Set the Content-Type for the topmost MIME part.
let _ = rest.add_header("Content-Type", "multipart/form-data");
// Specify each part of the request.
rest.set_part_selector("1");
let _ = rest.add_header("Content-Disposition", "form-data; name=\"text1\"");
let _ = rest.set_multipart_body_string("text 123 abc");
rest.set_part_selector("2");
let _ = rest.add_header("Content-Disposition", "form-data; name=\"text2\"");
let _ = rest.set_multipart_body_string("xyz");
rest.set_part_selector("3");
let _ = rest.add_header("Content-Disposition", "form-data; name=\"file1\"; filename=\"a.txt\"");
let _ = rest.add_header("Content-Type", "text/plain");
let _ = rest.set_multipart_body_string("Content of a.txt.");
rest.set_part_selector("4");
let _ = rest.add_header("Content-Disposition", "form-data; name=\"file2\"; filename=\"a.html\"");
let _ = rest.add_header("Content-Type", "text/html");
let sb_html = chilkat::StringBuilder::new();
let _ = sb_html.load_file("qa_data/html/a.html", "utf-8");
let _ = rest.set_multipart_body_sb(&sb_html);
rest.set_part_selector("5");
let _ = rest.add_header("Content-Disposition", "form-data; name=\"file3\"; filename=\"starfish.jpg\"");
let _ = rest.add_header("Content-Type", "image/jpeg");
// When the request is sent, stream this part directly from the file.
// This avoids having to load the entire file into memory.
let file_stream = chilkat::Stream::new();
file_stream.set_source_file("qa_data/jpg/starfish.jpg");
let _ = rest.set_multipart_body_stream(&file_stream);
let Ok(response_body) = rest.full_request_multipart("POST", "/some_path") else {
println!("{}", rest.last_error_text());
return;
};
// ...
// ...
// Clear the REST object for any subsequent requests..
let _ = rest.clear_all_headers();
let _ = rest.clear_all_parts();
rest.set_part_selector("");