I have a POST FastAPI method. I do not want to construct a class nor query string. So, I decide to apply Body()
method.
@app.post("/test-single-int")
async def test_single_int(
t: int = Body(...)
):
pass
This is the request
POST http://localhost:8000/test-single-int/
{
"t": 10
}
And this is the response
HTTP/1.1 422 Unprocessable Entity
date: Fri, 22 May 2020 10:00:16 GMT
server: uvicorn
content-length: 83
content-type: application/json
connection: close
{
"detail": [
{
"loc": [
"body",
"s"
],
"msg": "str type expected",
"type": "type_error.str"
}
]
}
However, after trying with many samples, I found that they will not error if I have more than one Body()
. For example,
@app.post("/test-multi-mix")
async def test_multi_param(
s: str = Body(...),
t: int = Body(...),
):
pass
Request
POST http://localhost:8000/test-multi-mix/
{
"s": "test",
"t": 10
}
Response
HTTP/1.1 200 OK
date: Fri, 22 May 2020 10:16:12 GMT
server: uvicorn
content-length: 4
content-type: application/json
connection: close
null
Does anyone have any idea about my implementation? Are there wrong? Is it not best practice? Or it is a bug?