python encoding_error ai_generated true

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

ID: python/fastapi-json-encoding-error-response

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2024-04-02First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

客户端发送的请求体不是有效的 JSON 格式,或 Content-Type 头未设置为 application/json。

generic

中文

客户端发送的请求体不是有效的 JSON 格式,或 Content-Type 头未设置为 application/json。

Workarounds

  1. 95% success
    import requests
    response = requests.post('http://localhost:8000/endpoint', json={'key': 'value'})

Dead Ends

Common approaches that don't work:

  1. 80% fail

    BOM 会导致 JSON 解析失败,因为 JSON 标准不允许 BOM

  2. 70% fail

    FastAPI 已经自动解析,手动解析会重复处理导致错误