python data_error ai_generated true

FastAPI 请求验证错误:值不是有效的整数

fastapi.exceptions.RequestValidationError: value is not a valid integer

ID: python/fastapi-query-parameter-validation

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

版本兼容性

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

根因分析

查询参数或路径参数的类型不匹配,例如传入了字符串而不是整数。

English

查询参数或路径参数的类型不匹配,例如传入了字符串而不是整数。

generic

解决方案

  1. 100% 成功率
    @app.get('/items/{item_id}')
    async def get_item(item_id: int):
        return {'item_id': item_id}

无效尝试

常见但无效的做法:

  1. 90% 失败

    FastAPI 会在函数执行前验证,转换无效

  2. 80% 失败

    会忽略验证错误,导致数据不一致