pub struct MultipartWriter<W: Write> { /* private fields */ }std only.Expand description
Writes a multipart/form-data body as browsers do.
Names and file names are escaped by the browser rule; every input that could read back differently is refused; text line breaks become CRLF. With a caller boundary, data is scanned for the delimiter across chunk splits and refused if it contains one. So every body the writer produces parses back with form-net to the same fields.
§Examples
let mut body = Vec::new();
// `MultipartWriter::new` (feature `random`) generates the boundary.
let boundary = form_net::Boundary::new(b"----form-net-example")?;
let mut w = form_net::MultipartWriter::with_boundary(&mut body, boundary);
w.text("title", "Holiday")?;
w.file("photo", "beach.jpg", Some("image/jpeg"), &b"\xff\xd8 jpeg"[..])?;
let content_type = w.content_type();
w.finish()?;
let mp = form_net::parse_multipart(content_type.as_bytes(), &body)?;
assert_eq!(mp.field("title"), Some("Holiday"));
assert_eq!(mp.file("photo").map(|p| p.bytes()), Some(&b"\xff\xd8 jpeg"[..]));Implementations§
Source§impl<W: Write> MultipartWriter<W>
impl<W: Write> MultipartWriter<W>
Sourcepub fn with_boundary(out: W, boundary: Boundary) -> Self
pub fn with_boundary(out: W, boundary: Boundary) -> Self
A writer with the caller’s boundary; data is scanned for it.
Sourcepub fn content_type(&self) -> String
pub fn content_type(&self) -> String
The Content-Type header value to send:
multipart/form-data; boundary=..., quoted when the boundary
holds a character that is not a token character.
Sourcepub fn verify_boundary(&mut self, on: bool)
pub fn verify_boundary(&mut self, on: bool)
Turns the delimiter scan on or off. It is on for a caller boundary and off for a generated one.
Sourcepub fn text(&mut self, name: &str, value: &str) -> Result<(), FormError>
pub fn text(&mut self, name: &str, value: &str) -> Result<(), FormError>
Writes a text field. Line breaks in value become CRLF. A refused
head writes nothing; after any error the output is incomplete and
must be discarded.
§Errors
The head errors of encode_part_head;
Syntax { BoundaryInContent }; Encoding { UnsupportedCharset }
for a _charset_ field naming anything but UTF-8 or US-ASCII
(the text after it would not read back); Io; Poisoned.
Sourcepub fn file_start(
&mut self,
name: &str,
filename: &str,
content_type: Option<&str>,
) -> Result<(), FormError>
pub fn file_start( &mut self, name: &str, filename: &str, content_type: Option<&str>, ) -> Result<(), FormError>
Sourcepub fn file_data(&mut self, chunk: &[u8]) -> Result<(), FormError>
pub fn file_data(&mut self, chunk: &[u8]) -> Result<(), FormError>
More data of the current file part.
§Errors
Syntax { BoundaryInContent }; Io; Poisoned outside a file
part.