Skip to main content

Form

Declare a form data parameter for a path operation. Form parameters are sent as application/x-www-form-urlencoded or multipart/form-data.

Signature

Parameters

Any
default:"Undefined"
Default value if the parameter field is not set.
str
default:"application/x-www-form-urlencoded"
The media type of this parameter field. Changing it would affect the generated OpenAPI, but currently it doesn’t affect the parsing of the data.
str | None
default:"None"
An alternative name for the parameter field. This will be used to extract the data and for the generated OpenAPI.
str | None
default:"None"
Human-readable title for the parameter.
str | None
default:"None"
Human-readable description for the parameter.
float | None
default:"None"
Greater than validation. If set, the value must be greater than this. Only applicable to numbers.
float | None
default:"None"
Greater than or equal validation. If set, the value must be greater than or equal to this. Only applicable to numbers.
float | None
default:"None"
Less than validation. If set, the value must be less than this. Only applicable to numbers.
float | None
default:"None"
Less than or equal validation. If set, the value must be less than or equal to this. Only applicable to numbers.
int | None
default:"None"
Minimum length for strings.
int | None
default:"None"
Maximum length for strings.
str | None
default:"None"
RegEx pattern for strings.
str | None
default:"None"
Parameter field name for discriminating the type in a tagged union.
bool | None
default:"None"
If True, strict validation is applied to the field.
float | None
default:"None"
Value must be a multiple of this. Only applicable to numbers.
bool | None
default:"None"
Allow inf, -inf, nan. Only applicable to numbers.
int | None
default:"None"
Maximum number of allowed digits for numbers.
int | None
default:"None"
Maximum number of decimal places allowed for numbers.
list[Any] | None
default:"None"
Example values for this field.
bool | str | None
default:"None"
Mark this parameter field as deprecated. It will affect the generated OpenAPI (visible at /docs).
bool
default:"True"
Whether to include this parameter field in the generated OpenAPI.
dict[str, Any] | None
default:"None"
Any additional JSON schema data.

Examples

Basic Form

To use form data, you need to install python-multipart: pip install python-multipart

Form with Validation

Optional Form Fields

Form with Default Values

Mixing Form and File

When mixing Form() and File(), the content type automatically becomes multipart/form-data.

Common Use Cases

Login Form

Registration Form

Search Form

Contact Form

Form vs JSON Body

You cannot use Form() and Body() in the same path operation. They use different content types (multipart/form-data vs application/json).
Form data is sent as key-value pairs:
JSON body is sent as JSON:

Installation Requirement

Form data requires the python-multipart package: