python
encoding_error
ai_generated
true
JSON 解码错误:期望值:第 1 行第 1 列(字符 0)
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ID: python/fastapi-json-encoding-error-response
80%修复率
82%置信度
0证据数
2024-04-02首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
客户端发送的请求体不是有效的 JSON 格式,或 Content-Type 头未设置为 application/json。
English
客户端发送的请求体不是有效的 JSON 格式,或 Content-Type 头未设置为 application/json。
解决方案
-
95% 成功率
import requests response = requests.post('http://localhost:8000/endpoint', json={'key': 'value'})
无效尝试
常见但无效的做法:
-
80% 失败
BOM 会导致 JSON 解析失败,因为 JSON 标准不允许 BOM
-
70% 失败
FastAPI 已经自动解析,手动解析会重复处理导致错误