URL-encoded forms
TheForm 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
TheForm 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 useForm to return form-encoded data:
Content-Type: application/x-www-form-urlencoded.
Multipart form data
For file uploads, use theMultipart extractor:
Basic file upload
Processing multipart fields
Saving uploaded files
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 likevalidator:
Testing form handlers
Common form issues and solutions
Common form issues and solutions
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.