pub struct Form { /* private fields */ }Expand description
A builder for a multipart/form-data request body.
Add fields via Part, then obtain the Content-Type header value with
Form::content_type and the body stream with [Form::into_stream].
§Examples
use http::Request;
use http_multipart::{Form, Part};
// construct a form from parts
let parts = vec![
Part::text("username", "alice"),
Part::binary("data", &b"hello"[..]).file_name("hello.bin"),
];
let form = Form::new(parts);
// build a request with the form
let req = Request::builder()
.method("POST") // multipart is usually used with POST method
.header("Content-Type", form.content_type()) // content type header must be set to form's content type
.body(form) // form implements Stream, so it can be used directly as the body
.unwrap();Implementations§
Source§impl Form
impl Form
Sourcepub fn new(parts: Vec<Part>) -> Self
pub fn new(parts: Vec<Part>) -> Self
Create a new form with an automatically generated boundary.
Sourcepub fn with_boundary(
boundary: impl AsRef<[u8]>,
parts: Vec<Part>,
) -> Result<Self, MultipartError>
pub fn with_boundary( boundary: impl AsRef<[u8]>, parts: Vec<Part>, ) -> Result<Self, MultipartError>
Create a form with a caller-supplied boundary.
Returns MultipartError::Boundary if boundary is empty or contains
\r / \n (which would break the wire format).
Sourcepub fn content_type(&self) -> HeaderValue
pub fn content_type(&self) -> HeaderValue
The Content-Type header value that must be set on the outgoing request.
Example: multipart/form-data; boundary=0000000000001a2b
Trait Implementations§
Source§impl Stream for Form
impl Stream for Form
Auto Trait Implementations§
impl Freeze for Form
impl !RefUnwindSafe for Form
impl Send for Form
impl !Sync for Form
impl Unpin for Form
impl UnsafeUnpin for Form
impl !UnwindSafe for Form
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more