python runtime_error ai_generated true

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

starlette.exceptions.HTTPException: 405: Method Not Allowed

ID: python/starlette-http-exception-405-method-not-allowed

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2024-06-18首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

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

English

The HTTP method used (e.g., POST) is not allowed for the requested endpoint.

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. Assuming all methods are allowed by default 80% 失败

    Starlette requires explicit method registration.

  2. Using GET when POST is required 70% 失败

    Method mismatch leads to 405.