python
data_error
ai_generated
true
fastapi.exceptions.RequestValidationError: Request 的 1 个校验错误 query -> q 字段缺失 (type=value_error.missing)
fastapi.exceptions.RequestValidationError: 1 validation error for Request query -> q field required (type=value_error.missing)
ID: python/fastapi-422-missing-query-param
80%修复率
86%置信度
0证据数
2024-03-29首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
未声明默认值的函数参数被视为必填查询参数,而客户端省略了它。
English
A function parameter declared without a default is treated as a required query parameter and the client omitted it.
解决方案
-
95% 成功率
from fastapi import Query @app.get('/search') async def search(q: str = Query(default='')): ... -
92% 成功率
async def search(q: str | None = None): ...
无效尝试
常见但无效的做法:
-
85% 失败
Still required; Query(...) means required.
-
70% 失败
Hides the contract mismatch from the client.