question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Regression since v0.44.1 - Error 422 if `Form` is declared before `File`

See original GitHub issue

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 ab2b86fe2ce8fe15e91aaec179438e24ff7b7ed0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:5

github_iconTop GitHub Comments

2reactions
AKlauscommented, Jan 11, 2022

Confirm that the issue still exists in FastAPI v0.70.1 (used Python v3.7.10).

Tested by running cURL (to avoid accusations towards Swagger):

curl --location --request POST 'http://127.0.0.1:5001/file_after_form' --form 'city="fff"' --form 'file=@"file.png"'

that gets HTTP code 422 with message:

{"detail":[{"loc":["body","file"],"msg":"byte type expected","type":"type_error.bytes"}]}

(I raised #4384 initially, but closing it as a duplicate).

1reaction
yifeikongcommented, May 25, 2021

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found