Regression since v0.44.1 - Error 422 if `Form` is declared before `File`
See original GitHub issueHere’s a branch with my use case:
from starlette.testclient import TestClient
from fastapi import FastAPI, File, Form
app = FastAPI()
@app.post("/file_before_form")
def file_before_form(
file: bytes = File(...), city: str = Form(...),
):
return {"file_content": file, "city": city}
@app.post("/file_after_form")
def file_after_form(
city: str = Form(...), file: bytes = File(...),
):
return {"file_content": file, "city": city}
client = TestClient(app)
def test_file_before_form():
response = client.post(
"/file_before_form", data={"city": "Thimphou"}, files={"file": "<file content>"}
)
assert response.status_code == 200, response.text
assert response.json() == {"file_content": "<file content>", "city": "Thimphou"}
def test_file_after_form():
response = client.post(
"/file_after_form", data={"city": "Thimphou"}, files={"file": "<file content>"}
)
assert response.status_code == 200, response.text # <-------------- this fails since ab2b86f
assert response.json() == {"file_content": "<file content>", "city": "Thimphou"}
Description
Upgrading FastApi from 0.42.0 to 0.61.0 on one of my project, I stumbled upon a regression in which FastApi will respond with HTTP Error 422 when ever posting a form-data request to an endpoint where a File
is declared after a Form
.
Searching the Internet, I found another user suffering from this issue : https://stackoverflow.com/questions/61376754/fastapi-files-must-be-loaded-before-form-in-function-parameters
I’ve created a branch with tests to showcase the issue : https://github.com/thomasleveil/fastapi/tree/file_and_form_together
Environment
- OS: Linux
- FastAPI Version 0.44.1 and later
- Python version: 3.8.5
Additional context
I’ve bisected the repo and figured out the regression was introduced in commit ab2b86fe2ce8fe15e91aaec179438e24ff7b7ed0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:7
- Comments:5
Top Results From Across the Web
How To Fix the HTTP 422 Error - Kinsta
Unlike other HTTP errors, the HTTP 422 code will keep reappearing until you manage to troubleshoot the problem. Here's how to fix it....
Read more >FastAPI returns "Error 422: Unprocessable entity" when I send ...
The 422 response body will contain an error message about which field(s) is missing or doesn't match the expected format. Since you haven't ......
Read more >ffmpeg video converter - manpages.ubuntu!
Filtering Before encoding, ffmpeg can process raw audio and video frames using filters from the libavfilter library. Several chained filters form a filter ......
Read more >https://origin-release.apps.ci.l2s4.p1.openshiftap...
... chore: fail earlier if creating virutal link failed before account creation ... disable match tags by default in account search when creating...
Read more >ED 025 365 - ERIC - Department of Education
Multiple Regression Analysis, Sot 2' Dependent Variables ... society as an interaction among the physical environment, forms of organization.
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
Confirm that the issue still exists in
FastAPI v0.70.1
(usedPython v3.7.10
).Tested by running cURL (to avoid accusations towards Swagger):
that gets HTTP code 422 with message:
(I raised #4384 initially, but closing it as a duplicate).
This is still not fixed as of May 2021(0.65.1), using
UploadFile
as the type seems to be ok, though. Any updates on this issue?