Issue 1:
First 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 2:
Here'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 ab2b86f