# UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 0 的字节 0x80：无效的起始字节

- **ID:** `python/fastapi-unicodedecodeerror-request-body`
- **领域:** python
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

请求体包含非 UTF-8 编码的数据，例如二进制或 Latin-1。

## 版本兼容性

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

## 解决方案

1. **Specify encoding when reading request body** (85% 成功率)
   ```
   data = await request.body()
text = data.decode('latin-1')  # or 'utf-8' with errors='replace'
   ```
2. **Use FastAPI's bytes type for raw data** (90% 成功率)
   ```
   from fastapi import UploadFile
@app.post('/upload')
async def upload(file: UploadFile):
    content = await file.read()
    # handle bytes as needed
   ```

## 无效尝试

- **Setting app default encoding to latin-1 globally** — Breaks other parts expecting UTF-8. (70% 失败率)
- **Ignoring errors with errors='ignore'** — Data loss can occur. (60% 失败率)
