pub fn parse_multipart<'a>(
content_type: &[u8],
body: &'a [u8],
) -> Result<Multipart<'a>, FormError>Available on crate feature
alloc only.Expand description
Parses a buffered multipart/form-data body with the default limits
and policy. Part data borrows the body (zero copy); a name borrows too,
except where an HTML escape had to be decoded.
Text fields are checked as text at parse time: a field that is not
valid UTF-8, or whose charset (its own, else the _charset_ field
before it, else UTF-8) is not UTF-8 or US-ASCII, refuses the body.
§Errors
Every multipart error; UnsupportedMediaType when the content type is
not multipart/form-data.
§Examples
let content_type = b"multipart/form-data; boundary=X";
let body = b"--X\r\n\
Content-Disposition: form-data; name=\"title\"\r\n\r\n\
Holiday\r\n\
--X\r\n\
Content-Disposition: form-data; name=\"avatar\"; filename=\"me.png\"\r\n\
Content-Type: image/png\r\n\r\n\
\x89PNG...\r\n\
--X--\r\n";
let mp = form_net::parse_multipart(content_type, body)?;
assert_eq!(mp.field("title"), Some("Holiday"));
let avatar = mp.file("avatar").unwrap();
assert_eq!(avatar.filename().unwrap().raw(), b"me.png");
assert!(avatar.content_type().unwrap().is("image", "png"));
assert_eq!(avatar.bytes(), b"\x89PNG...");