Skip to main content

MultipartForm

Struct MultipartForm 

Source
pub struct MultipartForm<T: MultipartCollect>(pub T);
Expand description

Typed multipart/form-data extractor.

To extract typed data from a multipart stream, the inner type T must implement the MultipartCollect trait. You should use the MultipartForm macro to derive this for your struct.

Note that this extractor rejects requests with any other Content-Type such as multipart/mixed, multipart/related, or non-multipart media types.

Add a MultipartFormConfig to your app data to configure extraction.

§Basic Use

Each field type should implement the FieldReader trait:

use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};

#[derive(MultipartForm)]
struct ImageUpload {
    description: Text<String>,
    timestamp: Text<i64>,
    image: TempFile,
}

§Optional and List Fields

You can also use Vec<T> and Option<T> provided that T: FieldReader.

A Vec field corresponds to an upload with multiple parts under the same field name.

use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};

#[derive(MultipartForm)]
struct Form {
    category: Option<Text<String>>,
    files: Vec<TempFile>,
}

§Field Renaming

You can use the #[multipart(rename = "foo")] attribute to receive a field by a different name.

use actix_multipart::form::{tempfile::TempFile, MultipartForm};

#[derive(MultipartForm)]
struct Form {
    #[multipart(rename = "files[]")]
    files: Vec<TempFile>,
}

§Field Limits

You can use the #[multipart(limit = "<size>")] attribute to set field level limits. The limit string is parsed using bytesize.

Note: the form is also subject to the global limits configured using MultipartFormConfig.

use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};

#[derive(MultipartForm)]
struct Form {
    #[multipart(limit = "2 KiB")]
    description: Text<String>,

    #[multipart(limit = "512 MiB")]
    files: Vec<TempFile>,
}

§Unknown Fields

By default fields with an unknown name are ignored. They can be rejected using the #[multipart(deny_unknown_fields)] attribute:

use actix_multipart::form::MultipartForm;

#[derive(MultipartForm)]
#[multipart(deny_unknown_fields)]
struct Form {}

§Duplicate Fields

The behaviour for when multiple fields with the same name are received can be changed using the #[multipart(duplicate_field = "<behavior>")] attribute:

  • “ignore”: (default) Extra fields are ignored. I.e., the first one is persisted.
  • “deny”: A MultipartError::DuplicateField error response is returned.
  • “replace”: Each field is processed, but only the last one is persisted.

Note that Vec fields will ignore this option.

use actix_multipart::form::MultipartForm;

#[derive(MultipartForm)]
#[multipart(duplicate_field = "deny")]
struct Form {}

Tuple Fields§

§0: T

Implementations§

Source§

impl<T: MultipartCollect> MultipartForm<T>

Source

pub fn into_inner(self) -> T

Unwrap into inner T value.

Trait Implementations§

Source§

impl<T: MultipartCollect> Deref for MultipartForm<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: MultipartCollect> DerefMut for MultipartForm<T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T> FromRequest for MultipartForm<T>
where T: MultipartCollect + 'static,

Source§

type Error = Error

The associated error which can be returned.
Source§

type Future = Pin<Box<dyn Future<Output = Result<MultipartForm<T>, <MultipartForm<T> as FromRequest>::Error>>>>

Future that resolves to a Self. Read more
Source§

fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future

Create a Self from request parts asynchronously.
Source§

fn extract(req: &HttpRequest) -> Self::Future

Create a Self from request head asynchronously. Read more

Auto Trait Implementations§

§

impl<T> Freeze for MultipartForm<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for MultipartForm<T>
where T: RefUnwindSafe,

§

impl<T> Send for MultipartForm<T>
where T: Send,

§

impl<T> Sync for MultipartForm<T>
where T: Sync,

§

impl<T> Unpin for MultipartForm<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for MultipartForm<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for MultipartForm<T>
where T: 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<R> CryptoRng for R
where R: TryCryptoRng<Error = Infallible> + ?Sized,

Source§

impl<T> Formattable for T
where T: Deref, <T as Deref>::Target: Formattable,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<R> Rng for R
where R: TryRng<Error = Infallible> + ?Sized,

Source§

fn next_u32(&mut self) -> u32

Return the next random u32.
Source§

fn next_u64(&mut self) -> u64

Return the next random u64.
Source§

fn fill_bytes(&mut self, dst: &mut [u8])

Fill dest with random data. Read more
Source§

impl<R> RngCore for R
where R: Rng,

Source§

impl<R> RngExt for R
where R: Rng + ?Sized,

Source§

fn random<T>(&mut self) -> T

Return a random value via the StandardUniform distribution. Read more
Source§

fn random_iter<T>(self) -> Iter<StandardUniform, Self, T>

Return an iterator over random variates Read more
Source§

fn random_range<T, R>(&mut self, range: R) -> T
where T: SampleUniform, R: SampleRange<T>,

Generate a random value in the given range. Read more
Source§

fn random_bool(&mut self, p: f64) -> bool

Return a bool with a probability p of being true. Read more
Source§

fn random_ratio(&mut self, numerator: u32, denominator: u32) -> bool

Return a bool with a probability of numerator/denominator of being true. Read more
Source§

fn sample<T, D>(&mut self, distr: D) -> T
where D: Distribution<T>,

Sample a new value, using the given distribution. Read more
Source§

fn sample_iter<T, D>(self, distr: D) -> Iter<D, Self, T>
where D: Distribution<T>, Self: Sized,

Create an iterator that generates values using the given distribution. Read more
Source§

fn fill<T>(&mut self, dest: &mut [T])
where T: Fill,

Fill any type implementing Fill with random data Read more
Source§

impl<R> TryCryptoRng for R
where R: DerefMut, <R as Deref>::Target: TryCryptoRng,

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<R> TryRng for R
where R: DerefMut, <R as Deref>::Target: TryRng,

Source§

type Error = <<R as Deref>::Target as TryRng>::Error

The type returned in the event of a RNG error. Read more
Source§

fn try_next_u32(&mut self) -> Result<u32, <R as TryRng>::Error>

Return the next random u32.
Source§

fn try_next_u64(&mut self) -> Result<u64, <R as TryRng>::Error>

Return the next random u64.
Source§

fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), <R as TryRng>::Error>

Fill dst entirely with random data.
Source§

impl<R> TryRngCore for R
where R: TryRng,

Source§

type Error = <R as TryRng>::Error

👎Deprecated since 0.10.0:

use TryRng instead

Error type.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more