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

- **ID:** `python/flask-unicode-decode-error`
- **领域:** python
- **类别:** encoding_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

`request.data.decode()` 或读取表单字段时假设 UTF-8，但客户端发送了其他编码（如 Windows-1252 或二进制）。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   charset = request.mimetype_params.get('charset', 'utf-8')
text = request.data.decode(charset)
   ```
2. **** (90% 成功率)
   ```
   try:
    text = request.data.decode('utf-8')
except UnicodeDecodeError:
    return jsonify(error='invalid encoding'), 400
   ```

## 无效尝试

- **** — Silently drops bytes; corrupts data. (75% 失败率)
- **** — Output config, not input decoding. (90% 失败率)
