🐛 Fix request body mediatype for form model with File fields#14343
Closed
YuriiMotov wants to merge 4 commits intofastapi:masterfrom
Closed
🐛 Fix request body mediatype for form model with File fields#14343YuriiMotov wants to merge 4 commits intofastapi:masterfrom
File fields#14343YuriiMotov wants to merge 4 commits intofastapi:masterfrom
Conversation
This reverts commit 57f0dde.
Contributor
📝 Docs previewLast commit f8a652c at: https://fd593ee8.fastapitiangolo.pages.dev Modified Pages |
svlandeg
reviewed
Nov 14, 2025
|
|
||
| You can also include **file fields** inside a form model by using `UploadFile` or `bytes` as field types and annotating them with `File`. | ||
|
|
||
| If there is at least one `File` field in the form model, **FastAPI** will automatically set the request content media type to `multipart/form-data` for this path operation. |
Member
There was a problem hiding this comment.
Should we add here a note to indicate from which version onwards, this is the case? Something like
Suggested change
| If there is at least one `File` field in the form model, **FastAPI** will automatically set the request content media type to `multipart/form-data` for this path operation. | |
| If there is at least one `File` field in the form model, **FastAPI** (from v.0.121.3 onwards) will automatically set the request content media type to `multipart/form-data` for this path operation. |
Member
|
I'm unsure about this. 🤔 I wouldn't expect people to create Pydantic models where the fields are declared with something other than Pydantic's regular |
This comment was marked as resolved.
This comment was marked as resolved.
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Currently if form parameter model contains
Filefield, the content mediatype for this path operation in openapi will be wrong:Request body content mediatype in openapi will be
application/x-www-form-urlencodedinstead ofmultipart/form-data:This PR enforces the content mediatype to be
multipart/form-datafor form parameter models that contain at least oneFilefield.I added
if request_media_type == "application/x-www-form-urlencoded"condition to make it possible to override mediatype by passing a parameter toForm: