Helper library for the axum framework
designed to allow you to parse the multipart/form-data
body of the
supplied request into an arbitrary struct.
Usage
Getting started
To get started you will need to define a struct with the desired fields and
implement the TryFromMultipart trait. In the vast
majority of cases you will want to use the derive macro to generate the
implementation automatically.
To be able to derive the implementation every field must implement the
TryFromField trait. The trait is
implemented by default for all primitive types, [String
], and [Vec<u8>
]
in case you just want to access the raw bytes.
If the request body is malformed or it does not contain the necessary data the request will be aborted with an error.
use ;
async
Optional fields
If a field is declared as an [Option] the value will default to [Option::None] when the field is missing from the request body.
use TryFromMultipart;
Renaming fields
If you would like to assign a custom name to the struct field you can use
the field_name
parameter in the form_data
attribute.
use TryFromMultipart;
Field metadata
If you need access to the field metadata (e.g. the request headers) you can use the FieldData struct to wrap your field.
use ;
async
Large uploads
For large file uploads you can save the contents of the file to the file system using the TempFile helper. This will stream the field body to the file system allowing you to save the contents later.
use ;
async