# 键错误：'Authorization'

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

## 根因

在 Starlette 中直接访问 request.headers['Authorization']，但请求未包含该头部。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   auth = request.headers.get('Authorization')
if auth is None:
    return JSONResponse({'error': 'missing'}, status_code=401)
   ```
2. **** (90% 成功率)
   ```
   auth = request.headers.get('Authorization', '')
   ```

## 无效尝试

- **** — 头部名称不区分大小写，但拼写错误会导致返回 None，后续逻辑出错。 (50% 失败率)
- **** — 未处理缺失头部的情况，可能导致未授权访问。 (60% 失败率)
