python data_error ai_generated true

fastapi.exceptions.RequestValidationError: 1 validation error for Body body -> username field required (type=value_error.missing)

ID: python/fastapi-missing-form-data

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100+ active — — —
0.110+ active — — —

Root Cause

Endpoint declares Form(...) but client sends JSON, or declares JSON body but client sends form-urlencoded.

generic

中文

端点声明了 Form(...) 但客户端发送 JSON,或声明了 JSON body 但客户端发送 form-urlencoded。

Workarounds

  1. 95% success
    pip install python-multipart  # required for Form/File
  2. 90% success
    async def login(username: str = Form(...), password: str = Form(...))  # and client must send application/x-www-form-urlencoded

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Server starts but Form parsing still fails at runtime.

  2. 65% fail

    Breaks form clients; still 422 for the other content type.