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
80%修复率
90%置信度
0证据数
2024-10-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
请求 URL 中未提供必需的查询参数。
English
A required query parameter is not provided in the request URL.
解决方案
-
95% 成功率
Provide a default value: def read_items(page: int = 1)
-
90% 成功率
Make it optional with Optional[int] = None and handle None in the function
无效尝试
常见但无效的做法:
-
30% 失败
If the default is set, FastAPI won't raise an error; the error occurs only when no default is given.
-
60% 失败
This handles the error but doesn't fix the missing parameter; the client still needs to send it.