[英]Why am I getting {"detail":[{"loc":["path","id"],"msg":"field required","type":"value_error.missing"}]} if I made query with params?
This is the endpoint that is not working:这是不起作用的端点:
@router.get(
"/{question_id}",
tags=["questions"],
status_code=status.HTTP_200_OK,
response_model=Question,
dependencies=[Depends(get_db)],
)
def get_question(id: int = Path(..., gt=0)):
return get_question_service(id)
This is what the server shows when I run the query from the interactive FastAPI docs:这是当我从交互式 FastAPI 文档运行查询时服务器显示的内容:
INFO: 127.0.0.1:45806 - "GET /api/v1/questions/%7Bquestion_id%7D HTTP/1.1" 422 Unprocessable
I don't know why it is sending {question_id}
here instead of the number.我不知道为什么它在这里发送
{question_id}
而不是数字。
Also when I run a query from curl, this is what the server shows:此外,当我从 curl 运行查询时,服务器显示如下:
INFO: 127.0.0.1:59104 - "GET /api/v1/questions/21 HTTP/1.1" 422 Unprocessable Entity
It makes no sense since I'm sending the only required param: (question_id)
这没有意义,因为我发送的是唯一需要的参数:
(question_id)
The other endpoint is working fine:另一个端点工作正常:
@router.get(
"/",
tags=["questions"],
status_code=status.HTTP_200_OK,
response_model=List[Question],
dependencies=[Depends(get_db)],
)
def get_questions():
return get_questions_service()
There is a mismatch between the path parameter in the path string and the function argument.路径字符串中的路径参数与 function 参数不匹配。 Rename the function argument to
question_id
将 function 参数重命名为
question_id
@router.get(
"/{question_id}",
tags=["questions"],
status_code=status.HTTP_200_OK,
response_model=Question,
dependencies=[Depends(get_db)],
)
def get_question(question_id: int = Path(..., gt=0)):
return get_question_service(question_id)
or the path parameter to id
:或
id
的路径参数:
@router.get(
"/{id}",
tags=["questions"],
status_code=status.HTTP_200_OK,
response_model=Question,
dependencies=[Depends(get_db)],
)
def get_question(id: int = Path(..., gt=0)):
return get_question_service(id)
Btw, ...
in Path
can be omitted.顺便说一句,
Path
中的...
可以省略。 id: int = Path(gt=0)
is equivalent to id: int = Path(gt=0)
id: int = Path(gt=0)
等同于id: int = Path(gt=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.