python type_error ai_generated true

FastAPI请求验证错误:查询参数 'page' 缺失 (类型=值错误.缺失)

fastapi.exceptions.RequestValidationError: field required (type=value_error.missing) for query parameter 'page'

ID: python/fastapi-missing-query-parameter

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2025-11-15首次发现

版本兼容性

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

根因分析

请求URL中缺少必需的查询参数。

English

A required query parameter is missing from the request URL.

generic

解决方案

  1. 95% 成功率 Include the query parameter in the request URL.
    GET /items?page=1
  2. 90% 成功率 Make the query parameter optional with a default value.
    async def read_items(page: int = 1):

无效尝试

常见但无效的做法:

  1. Adding the parameter to the path instead of query. 70% 失败

    Path parameters and query parameters are different; mixing them causes 404.

  2. Setting a default value but not making it optional in the function signature. 60% 失败

    If the parameter is required in the function, a default may not be used.