简体   繁体   English

如果我进行查询,为什么会得到 {"detail":[{"loc":["path","id"],"msg":"field required","type":"value_error.missing"}]}与参数?

[英]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.

相关问题 FastAPI POST - 错误 422 详细信息'': ( ( loc'':(body file msg'':field required'', type'': value_error missing))) - FastAPI POST - Error 422 detail'': ( ( loc'':(body file msg'':field required'', type'': value_error missing))) 嵌套 Pydantic Model 使用 FastAPI 返回错误:需要字段(type=value_error.missing) - Nested Pydantic Model return error with FastAPI : field required (type=value_error.missing) 为什么我会收到“缺少 1 个必需的位置参数:”错误? - Why am I getting a 'missing 1 required positional argument:' error? python: {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} - python : {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} 电话号码的 pydantic 自定义数据类型:value_error.missing - pydantic custom data type for phone number : value_error.missing Pydantic 嵌套 model 字段抛出 value_error.missing - Pydantic nested model field throws value_error.missing POST 请求响应 422 错误 {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} - POST request response 422 error {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]} 为什么我收到错误:__init__() 缺少 1 个必需的位置参数? - Why am I getting the error: __init__() missing 1 required positional argument? 为什么我会收到 missing 1 required positional argument: 'connectionList' 错误但可以打印 connectionList? - Why am i getting a missing 1 required positional argument: 'connectionList' error but can print connectionList? 为什么我会收到“此字段是必填项”。 在加载页面时 - why am I getting “This field is required.” on loading of the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM