Skip to main content
Extractor and response type for URL encoded form data.

Type signature

As an extractor

The Form extractor deserializes form data from the request. The behavior depends on the request method:
  • GET or HEAD requests: Form data is read from the query string (same as Query)
  • Other methods: Form data is read from the request body with Content-Type: application/x-www-form-urlencoded
This matches how HTML forms are sent by browsers by default.
Since parsing form data might require consuming the request body, the Form extractor must be last if there are multiple extractors in a handler. See the order of extractors.

Basic usage

Optional fields

Use Option for optional form fields:

Multiple values

Form fields with the same name can be collected into a Vec:

As a response

Form can encode any type that implements serde::Serialize as application/x-www-form-urlencoded:

Error handling

Extractor rejections

415
The request method is not GET/HEAD and the content-type is not application/x-www-form-urlencoded.
400
Failed to deserialize form data from query string (GET/HEAD requests).
422
Failed to deserialize form data from request body (other methods).

Example errors

GET vs POST behavior

The Form extractor automatically handles both query strings and request bodies:

Multipart forms

For multipart/form-data (file uploads), use the Multipart extractor instead:

URL encoding

Form data uses URL encoding where special characters are percent-encoded: