python type_error ai_generated true

fastapi.exceptions.RequestValidationError: 1 validation error for Request query -> page value is not a valid integer (type=type_error.integer)

ID: python/fastapi-query-parameter-type-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-03-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.9 active
3.10 active

Root Cause

客户端提供的查询参数 'page' 无法转换为整数类型

generic

中文

客户端提供的查询参数 'page' 无法转换为整数类型

Workarounds

  1. 95% success 提供有效的整数值
    GET /items?page=1
  2. 90% success 使用 Query 设置默认值
    from fastapi import Query
    @app.get('/items')
    def get_items(page: int = Query(1, ge=1)):
        ...

Dead Ends

Common approaches that don't work:

  1. 将参数类型改为 str 并手动转换 60% fail

    失去了自动验证功能,可能引入其他错误

  2. 在客户端忽略类型检查 80% fail

    服务器端仍然会验证并返回错误