python network_error ai_generated true

HTTP 400:CORS 预检请求失败,缺少 Access-Control-Request-Method。

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

ID: python/fastapi-cors-preflight-400

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2025-09-08首次发现

版本兼容性

版本状态引入弃用备注
0.100.x active — — —
0.110.x active — — —

根因分析

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

English

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

generic

解决方案

  1. 95% 成功率
    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% 成功率
    Verify order: middleware added later wraps earlier ones; CORS should be outermost.

无效尝试

常见但无效的做法:

  1. 80% 失败

    Tedious and misses middleware ordering; CORSMiddleware already handles preflight.

  2. 90% 失败

    Browsers reject wildcard with credentials.