Request with Forms and Files requires the 'File' parameter to be listed first
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
@app.post( '/test-fail')
def test( param1: str = Form(...), # It fails if the File goes first
file: bytes = File(...)
):
return Response(content=file, media_type="application/octet-stream")
@app.post( '/test-success')
def test( file: bytes = File(...), # It succeeds if the File goes first
param1: str = Form(...)
):
return Response(content=file, media_type="application/octet-stream")
Description
BUG
The File
parameter on the POST method must go first, before other Form
parameters. Otherwise the end-point returns HTTP code 422.
Details A command line like below for the end-points from the example
curl --location --request POST 'http://127.0.0.1:5001/test' --form 'param1="fff"' --form 'file=@"file.png"'
would return HTTP code 200 with an expected result for /test-success
and HTTP code 422 for /test-fail
with the following JSON:
{"detail":[{"loc":["body","file"],"msg":"byte type expected","type":"type_error.bytes"}]}
Expected behaviour Validation of parameters doesn’t depend on the order of the parameters
Operating System
macOS
Operating System Details
v12.0.1
FastAPI Version
0.70.1
Python Version
3.7.10
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top Results From Across the Web
Request Forms and Files - FastAPI
You can define files and form fields at the same time using File and Form . Info. To receive uploaded files and/or form...
Read more >Approve a File Uploaded via Microsoft Forms
Step #1 – Upload the file using Microsoft Forms The first step is to ... For the trigger the only parameter you need...
Read more >Sending form data - Learn web development | MDN
If you want to send files, you need to take three extra steps: Set the method attribute to POST because file content can't...
Read more >How to send a "multipart/form-data" with requests in python?
In addition, it can stream the request from open file objects, ... You need to use the files parameter to send a multipart...
Read more >MS Forms & Power Automate - Part 3: Getting file content
In this video, we'll look at how to get the contents of files that have been submitted through a File Upload question on...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Closing as a duplicate of #1964.
Thank you @harunyasar for pointing out to that ticket! (I question my searching skills)
Apparently, I am mistaken.
https://github.com/tiangolo/fastapi/issues/1964