# JSON 解码错误：期望值：第 1 行第 1 列（字符 0）

- **ID:** `python/starlette-request-body-json-error`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

请求体为空或不是有效的 JSON，而端点尝试将其解析为 JSON。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (90% 成功率)
   ```
   Validate content-type and body: if request.headers.get('content-type') == 'application/json' and await request.body(): data = await request.json() else: return JSONResponse({'error': 'Invalid JSON'}, status_code=400)
   ```
2. **** (95% 成功率)
   ```
   Use FastAPI's body parameter with Pydantic model to automatically handle validation errors
   ```

## 无效尝试

- **** — Catches the error but doesn't provide a meaningful response; the client still gets a 500. (50% 失败率)
- **** — body() returns bytes; you still need to decode and parse, same issue if empty. (60% 失败率)
