python data_error ai_generated true

FastAPI 请求验证错误:查询参数 'page' 缺失

fastapi.exceptions.RequestValidationError: [{'loc': ['query', 'page'], 'msg': 'field required', 'type': 'value_error.missing'}]

ID: python/fastapi-query-param-missing

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-10-05首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

请求 URL 中未提供必需的查询参数。

English

A required query parameter is not provided in the request URL.

generic

解决方案

  1. 95% 成功率
    Provide a default value: def read_items(page: int = 1)
  2. 90% 成功率
    Make it optional with Optional[int] = None and handle None in the function

无效尝试

常见但无效的做法:

  1. 30% 失败

    If the default is set, FastAPI won't raise an error; the error occurs only when no default is given.

  2. 60% 失败

    This handles the error but doesn't fix the missing parameter; the client still needs to send it.