Skip to main content
Axum provides extractors for handling both URL-encoded forms and multipart form data (commonly used for file uploads).

URL-encoded forms

The Form extractor deserializes form data from requests:

Basic form handling

1

Define your form data structure

Create a struct that implements Deserialize:
2

Use the Form extractor

Extract form data in your handler:
3

Create routes

Set up GET and POST routes for your form:

Form data sources

The Form extractor automatically handles data from different sources based on the request method:
For POST requests, the Form extractor requires the Content-Type header to be application/x-www-form-urlencoded. Otherwise, it will reject the request with a 415 Unsupported Media Type error.

Complete form example

Form responses

You can also use Form to return form-encoded data:
This will automatically set Content-Type: application/x-www-form-urlencoded.

Multipart form data

For file uploads, use the Multipart extractor:

Basic file upload

Processing multipart fields

Saving uploaded files

Always validate and sanitize file names before saving to prevent directory traversal attacks.

Handling mixed form fields

Multipart forms can contain both files and regular fields:

File size limits

By default, Axum limits request bodies to 2MB. Configure this for file uploads:
The DefaultBodyLimit::disable() must come before RequestBodyLimitLayer in the middleware chain.

Form validation

Integrate with validation libraries like validator:

Testing form handlers

Missing Content-Type header: POST forms require Content-Type: application/x-www-form-urlencoded. Check your client is sending this header.Form extractor must be last: If you have multiple extractors, Form and Multipart must come last since they consume the request body.Field name mismatch: Ensure your struct field names match the HTML form field names, or use serde’s #[serde(rename = "...")] attribute.Large file uploads fail: Increase the body size limit using DefaultBodyLimit and RequestBodyLimitLayer.