python
data_error
ai_generated
true
JSON 解码错误:期望值:第 1 行第 1 列(字符 0)
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
ID: python/starlette-request-body-parse-error
80%修复率
84%置信度
0证据数
2025-06-15首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
| 3.11 | active | — | — | — |
根因分析
Starlette 收到的请求体不是有效的 JSON 格式
English
Starlette 收到的请求体不是有效的 JSON 格式
解决方案
-
95% 成功率 验证请求体格式并返回错误
try: data = await request.json() except JSONDecodeError: return JSONResponse({'error': '无效的 JSON'}, status_code=400) -
90% 成功率 使用 Pydantic 模型自动验证
from pydantic import BaseModel class Item(BaseModel): name: str item = Item.parse_raw(await request.body())
无效尝试
常见但无效的做法:
-
忽略错误并返回空数据
80% 失败
可能接受无效请求
-
使用 eval 解析请求体
95% 失败
存在严重安全风险