Bird
0
0

Identify the error in this FastAPI CORS middleware setup:

medium📝 Debug Q14 of 15
FastAPI - Middleware and Hooks
Identify the error in this FastAPI CORS middleware setup:
app.add_middleware(
    CORSMiddleware,
    allow_origins="*",
    allow_methods=["GET", "POST"],
    allow_headers=["*"]
)
ACORSMiddleware must be imported from fastapi.middleware.security
Ballow_methods should be a string, not a list
Callow_headers cannot contain '*'
Dallow_origins should be a list, not a string
Step-by-Step Solution
Solution:
  1. Step 1: Check allow_origins type

    allow_origins must be a list of strings, but here it is a single string "*".
  2. Step 2: Verify other parameters

    allow_methods is correctly a list, allow_headers can accept ["*"] as a list.
  3. Final Answer:

    allow_origins should be a list, not a string -> Option D
  4. Quick Check:

    allow_origins requires a list [OK]
Quick Trick: Always use a list for allow_origins, even if one item [OK]
Common Mistakes:
MISTAKES
  • Passing allow_origins as a string instead of list
  • Misunderstanding allow_methods type
  • Wrong import path for CORSMiddleware

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes