Bird
0
0

You want to allow only two specific websites to access your FastAPI API with all HTTP methods and headers. Which CORS middleware setup is correct?

hard📝 component behavior Q8 of 15
FastAPI - Middleware and Hooks
You want to allow only two specific websites to access your FastAPI API with all HTTP methods and headers. Which CORS middleware setup is correct?
Aapp.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
Bapp.add_middleware(CORSMiddleware, allow_origins="https://site1.com,https://site2.com", allow_methods=["GET", "POST"], allow_headers=["*"])
Capp.add_middleware(CORSMiddleware, allow_origins=["https://site1.com", "https://site2.com"], allow_methods=["*"], allow_headers=["*"])
Dapp.add_middleware(CORSMiddleware, allow_origins=["https://site1.com", "https://site2.com"], allow_methods="*", allow_headers="*")
Step-by-Step Solution
Solution:
  1. Step 1: Check allow_origins format

    Must be a list of allowed origins, so list with two URLs is correct.
  2. Step 2: Check allow_methods and allow_headers

    Using ["*"] allows all methods and headers, which matches requirement.
  3. Final Answer:

    app.add_middleware(CORSMiddleware, allow_origins=["https://site1.com", "https://site2.com"], allow_methods=["*"], allow_headers=["*"]) -> Option C
  4. Quick Check:

    List origins + wildcard methods/headers = C [OK]
Quick Trick: Use list for origins; ["*"] for all methods and headers [OK]
Common Mistakes:
MISTAKES
  • Passing origins as string
  • Using string instead of list for methods/headers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes