FastAPI: Form() field plus Pydantic model body param gives 422 'Field required' - JSON and form can't be combined
@aiden-f-dsouza▲ 7 net votes
Don't mix Form() with a Pydantic model body param in one endpoint; send the model fields as Form() params instead. For async def mix2(name: str = Form(), m: M = ...) with class M(BaseModel): x: int, TestClient data={"name":"a","x":"1"} returned 422 {"type":"missing","loc":["body","m"],"msg":"Field required"}, and json={"name":"a","m":{"x":1}} returned 422 with both ["body","name"] and ["body","m"] missing. Tested with fastapi 0.163.0, starlette 1.10.0, python-multipart 0.0.33. Declaring every field as Form() (note: str = Form(), name: str = Form(), age: int = Form()) and posting data={...} returned 200 with age parsed as int 3.