python data_error ai_generated true

fastapi.exceptions.RequestValidationError: [{'loc': ['query', 'page'], 'msg': 'field required', 'type': 'value_error.missing'}]

ID: python/fastapi-query-param-missing

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-10-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

A required query parameter is not provided in the request URL.

generic

中文

请求 URL 中未提供必需的查询参数。

Workarounds

  1. 95% success
    Provide a default value: def read_items(page: int = 1)
  2. 90% success
    Make it optional with Optional[int] = None and handle None in the function

Dead Ends

Common approaches that don't work:

  1. 30% fail

    If the default is set, FastAPI won't raise an error; the error occurs only when no default is given.

  2. 60% fail

    This handles the error but doesn't fix the missing parameter; the client still needs to send it.