# fastapi.exceptions.HTTPException: 422：无法处理的实体 - json_invalid：JSON 解码错误

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

## 根因

请求 Content-Type 为 application/json，但请求体是格式错误的 JSON（尾随逗号、单引号、空体）。

## 版本兼容性

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

## 解决方案

1. **** (93% 成功率)
   ```
   import json
body = json.dumps({'name': 'x'})
requests.post(url, data=body, headers={'Content-Type': 'application/json'})
   ```
2. **** (95% 成功率)
   ```
   requests.post(url, json={'name': 'x'})
   ```

## 无效尝试

- **** — Accepting non-JSON violates the API contract. (85% 失败率)
- **** — FastAPI then rejects because the endpoint expects a Pydantic body. (90% 失败率)
