# starlette.exceptions.HTTPException: 405: 方法不允许

- **ID:** `python/starlette-http-exception-405-method-not-allowed`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用的 HTTP 方法（例如 POST）不允许用于请求的端点。

## 版本兼容性

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

## 解决方案

1. **Define route with correct methods** (90% 成功率)
   ```
   from starlette.routing import Route
async def handle(request):
    return PlainTextResponse('OK')
app.add_route('/data', handle, methods=['POST'])
   ```
2. **Use method-specific decorators** (95% 成功率)
   ```
   @app.post('/data')
async def create(request):
    return JSONResponse({'status': 'created'})
   ```

## 无效尝试

- **Assuming all methods are allowed by default** — Starlette requires explicit method registration. (80% 失败率)
- **Using GET when POST is required** — Method mismatch leads to 405. (70% 失败率)
