【发布时间】:2020-09-09 03:58:37
【问题描述】:
我有一个 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
有人对我的实现有任何想法吗?有错吗?这不是最佳实践吗?或者它是一个错误?
【问题讨论】:
标签: python http-post httprequest fastapi pydantic