python data_error ai_generated true

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

ID: python/fastapi-query-parameter-validation

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-09-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 80% fail

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