python network_error ai_generated true

HTTP/1.1 400 Bad Request CORS preflight request failed: missing Access-Control-Request-Method

ID: python/fastapi-cors-preflight-400

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-09-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100.x active — — —
0.110.x active — — —

Root Cause

CORSMiddleware not added, or added after other middleware that short-circuits OPTIONS; or allow_methods excludes the method.

generic

中文

未添加 CORSMiddleware,或在会短路 OPTIONS 的中间件之后添加;或 allow_methods 未包含该方法。

Workarounds

  1. 95% success
    app.add_middleware(CORSMiddleware, allow_origins=['https://app.example.com'], allow_methods=['GET','POST','PUT','DELETE','OPTIONS'], allow_headers=['*'], allow_credentials=True) — add FIRST before other middlewares.
  2. 90% success
    Verify order: middleware added later wraps earlier ones; CORS should be outermost.

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Tedious and misses middleware ordering; CORSMiddleware already handles preflight.

  2. 90% fail

    Browsers reject wildcard with credentials.