Struct FormData

Source
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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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());

Trait Implementations§

Source§

impl<W: Clone> Clone for FormData<W>

Source§

fn clone(&self) -> FormData<W>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<W: Debug> Debug for FormData<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<W> Freeze for FormData<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for FormData<W>
where W: RefUnwindSafe,

§

impl<W> Send for FormData<W>
where W: Send,

§

impl<W> Sync for FormData<W>
where W: Sync,

§

impl<W> Unpin for FormData<W>
where W: Unpin,

§

impl<W> UnwindSafe for FormData<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V