Bird
0
0

Examine the following FastAPI CORS middleware setup:

medium📝 Debug Q7 of 15
FastAPI - Middleware and Hooks
Examine the following FastAPI CORS middleware setup:
app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://example.com"],
    allow_methods="GET,POST",
    allow_headers=["*"]
)

What is the issue with this configuration?
Aallow_origins cannot contain a single URL in a list.
Ballow_methods should be a list of methods, not a comma-separated string.
Callow_headers cannot contain wildcard '*' in FastAPI.
DCORSMiddleware must be imported from fastapi.middleware.cors, not fastapi.middleware.
Step-by-Step Solution
Solution:
  1. Step 1: Check allow_methods type

    allow_methods must be a list of HTTP methods, e.g., ["GET", "POST"].
  2. Step 2: Identify the error

    Here, allow_methods is a string "GET,POST", which is invalid.
  3. Step 3: Verify other parameters

    allow_origins as a list with one URL is valid; allow_headers with ["*"] is allowed.
  4. Final Answer:

    allow_methods should be a list of methods, not a comma-separated string. -> Option B
  5. Quick Check:

    Ensure allow_methods is a list, not a string. [OK]
Quick Trick: allow_methods must be a list, not a comma-separated string. [OK]
Common Mistakes:
MISTAKES
  • Passing allow_methods as a string instead of a list.
  • Misunderstanding that allow_origins must be a list even for one origin.
  • Thinking wildcard '*' is invalid in allow_headers.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes