pub struct Form<'a> { /* private fields */ }Expand description
Implements the multipart/form-data media type as described by RFC 7578.
See.
Implementations§
Source§impl<'a> Form<'a>
impl<'a> Form<'a>
Sourcepub fn new<G>() -> Selfwhere
G: BoundaryGenerator,
pub fn new<G>() -> Selfwhere
G: BoundaryGenerator,
Creates a new form with the specified boundary generator function.
§Examples
struct TestGenerator;
impl BoundaryGenerator for TestGenerator {
fn generate_boundary() -> String {
"test".to_string()
}
}
let form = Form::new::<TestGenerator>();Sourcepub fn add_text<N, T>(&mut self, name: N, text: T)
pub fn add_text<N, T>(&mut self, name: N, text: T)
Adds a text part to the Form.
§Examples
use multipart_rfc7578::Form;
let mut form = Form::default();
form.add_text("text", "Hello World!");
form.add_text("more", String::from("Hello Universe!"));Sourcepub fn add_reader2<F, G, R>(
&mut self,
name: F,
read: R,
filename: Option<G>,
mime: Option<Mime>,
length: Option<u64>,
)
pub fn add_reader2<F, G, R>( &mut self, name: F, read: R, filename: Option<G>, mime: Option<Mime>, length: Option<u64>, )
Adds a readable part to the Form.
§Examples
use multipart_rfc7578::Form;
use std::io::Cursor;
let string = "Hello World!";
let bytes = Cursor::new(string);
let mut form = Form::default();
form.add_reader2("input", bytes, Some("filename.png"), Some(mime::TEXT_PLAIN), Some(string.len() as u64));Sourcepub fn add_reader<F, R>(&mut self, name: F, read: R)
pub fn add_reader<F, R>(&mut self, name: F, read: R)
Adds a readable part to the Form.
§Examples
use multipart_rfc7578::Form;
use std::io::Cursor;
let bytes = Cursor::new("Hello World!");
let mut form = Form::default();
form.add_reader("input", bytes);Sourcepub fn add_reader_file<F, G, R>(&mut self, name: F, read: R, filename: G)
pub fn add_reader_file<F, G, R>(&mut self, name: F, read: R, filename: G)
Adds a readable part to the Form as a file.
§Examples
use multipart_rfc7578::Form;
use std::io::Cursor;
let bytes = Cursor::new("Hello World!");
let mut form = Form::default();
form.add_reader_file("input", bytes, "filename.txt");Sourcepub fn add_reader_file_with_mime<F, G, R>(
&mut self,
name: F,
read: R,
filename: G,
mime: Mime,
)
pub fn add_reader_file_with_mime<F, G, R>( &mut self, name: F, read: R, filename: G, mime: Mime, )
Adds a readable part to the Form as a file with a specified mime.
§Examples
use multipart_rfc7578::Form;
use std::io::Cursor;
let bytes = Cursor::new("Hello World!");
let mut form = Form::default();
form.add_reader_file_with_mime("input", bytes, "filename.txt", mime::TEXT_PLAIN);Sourcepub fn add_file<P, F>(&mut self, name: F, path: P) -> Result<()>
pub fn add_file<P, F>(&mut self, name: F, path: P) -> Result<()>
Adds a file, and attempts to derive the mime type.
§Examples
use multipart_rfc7578::Form;
let mut form = Form::default();
form.add_file("file", file!()).expect("file to exist");Sourcepub fn add_file_with_mime<P, F>(
&mut self,
name: F,
path: P,
mime: Mime,
) -> Result<()>
pub fn add_file_with_mime<P, F>( &mut self, name: F, path: P, mime: Mime, ) -> Result<()>
Adds a file with the specified mime type to the form. If the mime type isn’t specified, a mime type will try to be derived.
§Examples
use multipart_rfc7578::Form;
let mut form = Form::default();
form.add_file_with_mime("data", "test.csv", mime::TEXT_CSV);Sourcepub fn content_type(&self) -> String
pub fn content_type(&self) -> String
get boundary as content type string
Sourcepub fn content_length(&self) -> Option<u64>
pub fn content_length(&self) -> Option<u64>
get content length
Source§impl Form<'static>
impl Form<'static>
Sourcepub fn set_body(self) -> !
pub fn set_body(self) -> !
Just for documentation. Updates a request instance with the multipart Content-Type header and the payload data.
§awc example
use awc::Client;
use multipart_rfc7578::{Form, SetBody};
let url = "http://localhost:80/upload";
let req = Client::default().post(url);
let mut form = Form::default();
form.add_text("text", "Hello World!");
let req = form.set_body(req).unwrap();§Hyper example
use hyper::{Method, Request, Uri};
use multipart_rfc7578::{Form, SetBody};
let url: Uri = "http://localhost:80/upload".parse().unwrap();
let mut req_builder = Request::post(url);
let mut form = Form::default();
form.add_text("text", "Hello World!");
let req = form.set_body(&mut req_builder).unwrap();Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Form<'a>
impl<'a> !RefUnwindSafe for Form<'a>
impl<'a> Send for Form<'a>
impl<'a> !Sync for Form<'a>
impl<'a> Unpin for Form<'a>
impl<'a> !UnwindSafe for Form<'a>
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