python data_error ai_generated true

fastapi.exceptions.RequestValidationError: JSON 解码错误:期望值:第 1 行第 1 列 (字符 0)

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

ID: python/fastapi-json-decode-error

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-08-14首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 85% 失败

    Header alone does not provide parseable JSON; still fails.

  2. 90% 失败

    Parsing occurs before the handler in FastAPI.