python data_error ai_generated true

fastapi.exceptions.RequestValidationError: JSON decode error Expecting value: line 1 column 1 (char 0)

ID: python/fastapi-json-decode-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-08-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

The client sent an empty or non-JSON body while the endpoint expects application/json.

generic

中文

客户端发送了空请求体或非 JSON 请求体,而端点期望 application/json。

Workarounds

  1. 92% success
    async def create(item: Item | None = None): ...
  2. 88% success
    from fastapi import Body
    async def create(item: Item = Body(default=None)): ...

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Header alone does not provide parseable JSON; still fails.

  2. 90% fail

    Parsing occurs before the handler in FastAPI.