#[derive(MultipartForm)]
{
    // Attributes available to this derive:
    #[multipart]
}
Expand description

Implements the MultipartFormTrait for a struct so that it can be used with the MultipartForm extractor.

Simple Example

Each field type should implement the FieldReader trait:

#[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.

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

Field Renaming

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

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

Field Limits

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

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

#[derive(MultipartForm)]
struct Form {
    #[multipart(limit="2KiB")]
    description: Text<String>,
    #[multipart(limit="512MiB")]
    files: Vec<Tempfile>,
}

Unknown Fields

By default fields with an unknown name are ignored. You can change this using the #[multipart(deny_unknown_fields)] attribute:

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

Duplicate Fields

You can change the behaviour for when multiple fields are received with the same name using the #[multipart(duplicate_action = "")] attribute:

  • “ignore”: Extra fields are ignored (default).
  • “replace”: Each field is processed, but only the last one is persisted.
  • “deny”: An Error::UnsupportedField error is returned.

(Note this option does not apply to Vec fields)

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