我有一个 POST FastAPI 方法。我不想构造一个类也不想查询字符串。所以,我决定应用Body()
方法。
@app.post("/test-single-int")
async def test_single_int(
t: int = Body(...)
):
pass
这是请求
POST http://localhost:8000/test-single-int/
{
"t": 10
}
这就是回应
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"
}
]
}
但是,在尝试了许多样本后,我发现如果我有多个样本,它们不会出错Body()
。例如,
@app.post("/test-multi-mix")
async def test_multi_param(
s: str = Body(...),
t: int = Body(...),
):
pass
要求
POST http://localhost:8000/test-multi-mix/
{
"s": "test",
"t": 10
}
回复
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
有人对我的实施有任何想法吗?有错吗?这不是最佳实践吗?或者它是一个错误?