python runtime_error ai_generated true

starlette.exceptions.HTTPException: 405: Method Not Allowed

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-06-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

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

generic

中文

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

Workarounds

  1. 90% success 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% success Use method-specific decorators
    @app.post('/data')
    async def create(request):
        return JSONResponse({'status': 'created'})

Dead Ends

Common approaches that don't work:

  1. Assuming all methods are allowed by default 80% fail

    Starlette requires explicit method registration.

  2. Using GET when POST is required 70% fail

    Method mismatch leads to 405.