pub struct FormData<W> { /* private fields */ }
Expand description
multipart/form-data
document builder.
See the module documentation for an example.
Implementations§
Source§impl<W: Write> FormData<W>
impl<W: Write> FormData<W>
Sourcepub fn new(writer: W) -> FormData<W>
pub fn new(writer: W) -> FormData<W>
Starts writing a multipart/form-data
document to writer
.
let mut form = FormData::new(Vec::new());
This generates a nonce as a multipart boundary by combining the current system time with a random string.
§Panics
Panics if the random number generator fails or if the current system time is prior to the Unix epoch.
Sourcepub fn finish(&mut self) -> Result<W>
pub fn finish(&mut self) -> Result<W>
Finish the multipart/form-data
document, returning the writer.
let mut form = FormData::new(Vec::new());
// ... things happen ...
let document: Vec<u8> = form.finish()?;
§Errors
Returns an error if finish()
has already been called or if the writer fails.
Sourcepub fn write_field(&mut self, name: &str, value: &str) -> Result<()>
pub fn write_field(&mut self, name: &str, value: &str) -> Result<()>
Write a non-file field to the document.
form.write_field("butts", "lol")?;
§Errors
Returns an error if finish()
has already been called or if the writer fails.
Sourcepub fn write_file<R: Read>(
&mut self,
name: &str,
reader: R,
filename: Option<&OsStr>,
content_type: &str,
) -> Result<()>
pub fn write_file<R: Read>( &mut self, name: &str, reader: R, filename: Option<&OsStr>, content_type: &str, ) -> Result<()>
Write a file field to the document, copying the data from reader
.
RFC 7578 § 4.2 advises “a name for the file SHOULD be supplied”, but “isn’t mandatory for cases where the file name isn’t availbale or is meaningless or private”.
use std::io::Cursor;
const CORRO: &[u8] = include_bytes!("../testdata/corro.svg");
form.write_file("corro", Cursor::new(CORRO), None, "image/svg+xml")?;
§Errors
Returns an error if finish()
has already been called or if the writer fails.
Sourcepub fn write_path<P: AsRef<Path>>(
&mut self,
name: &str,
path: P,
content_type: &str,
) -> Result<()>
pub fn write_path<P: AsRef<Path>>( &mut self, name: &str, path: P, content_type: &str, ) -> Result<()>
Write a file field to the document, opening the file at path
and copying its data.
This method detects the filename
parameter from the path
. To avoid this, use
FormData::write_file
.
form.write_path("corro", "testdata/corro.svg", "image/svg+xml")?;
§Errors
Returns an error if finish()
has already been called or if the writer fails.
Sourcepub fn content_type_header(&self) -> String
pub fn content_type_header(&self) -> String
Returns the value of the Content-Type
header that corresponds with the document.
// your HTTP client's API may vary
request.with_header("Content-Type", form.content_type_header());