python
data_error
ai_generated
true
fastapi.exceptions.RequestValidationError: 请求出现 1 个验证错误 query -> page 输入应为有效整数,无法将字符串解析为整数
fastapi.exceptions.RequestValidationError: 1 validation error for Request query -> page Input should be a valid integer, unable to parse string as an integer
ID: python/fastapi-request-validation-query
80%修复率
87%置信度
0证据数
2024-07-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 0.100+ | active | — | — | — |
根因分析
声明为 int 的查询参数收到了客户端传来的非数字字符串(通常是空值或 'abc')。
English
A query parameter declared as int received a non-numeric string (often empty or 'abc') from the client.
解决方案
-
95% 成功率
from fastapi import Query @app.get('/items') async def items(page: int = Query(1, ge=1)): ... -
85% 成功率
@app.exception_handler(RequestValidationError) async def handler(req, exc): return JSONResponse(status_code=422, content={'errors': exc.errors()})
无效尝试
常见但无效的做法:
-
85% 失败
Clients keep sending bad data and pagination logic receives None.
-
70% 失败
Moves the failure to an uncaught ValueError deeper in the stack.