Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does CORS stand for and why is it important in web development?
CORS stands for Cross-Origin Resource Sharing. It is important because it controls how resources on a web server can be requested from another domain, helping to keep web applications secure by preventing unauthorized cross-origin requests.
Click to reveal answer
beginner
How do you add CORS middleware in a FastAPI application?
You add CORS middleware by importing CORSMiddleware from fastapi.middleware.cors and then using app.add_middleware() with CORSMiddleware, specifying allowed origins, methods, headers, and optionally allow_credentials.
Click to reveal answer
beginner
What is the purpose of the 'allow_origins' parameter in FastAPI's CORSMiddleware?
'allow_origins' defines which domains are allowed to make cross-origin requests to your FastAPI app. It can be a list of URLs or ['*'] to allow all origins.
Click to reveal answer
intermediate
Why should you avoid setting 'allow_origins' to ['*'] in production?
Setting 'allow_origins' to ['*'] allows any website to access your API, which can be a security risk. It's better to specify trusted domains to limit access.
Click to reveal answer
beginner
Show a simple example of CORS middleware setup in FastAPI allowing only 'https://example.com'.
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["https://example.com"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# This setup allows only https://example.com to access the API.
Click to reveal answer
What does the 'allow_methods' parameter in FastAPI's CORSMiddleware control?
AWhich HTTP methods are allowed for cross-origin requests
BWhich domains can access the API
CWhether cookies are allowed
DThe maximum request size
✗ Incorrect
'allow_methods' specifies which HTTP methods (GET, POST, etc.) are allowed in cross-origin requests.
Which FastAPI import is needed to add CORS middleware?
A. CORSMiddleware must be imported from fastapi.middleware.security
B. allow_methods should be a string, not a list
C. allow_headers cannot contain '*'
D. allow_origins should be a list, not a string
Solution
Step 1: Check allow_origins type
allow_origins must be a list of strings, but here it is a single string "*".
Step 2: Verify other parameters
allow_methods is correctly a list, allow_headers can accept ["*"] as a list.
Final Answer:
allow_origins should be a list, not a string -> Option D
Quick Check:
allow_origins requires a list [OK]
Hint: Always use a list for allow_origins, even if one item [OK]
Common Mistakes:
Passing allow_origins as a string instead of list
Misunderstanding allow_methods type
Wrong import path for CORSMiddleware
5. You want your FastAPI backend to accept requests from two frontend domains: https://app1.example.com and https://app2.example.com. You also want to allow all HTTP methods and headers. Which CORS middleware setup is correct?
hard
A. app.add_middleware(CORSMiddleware, allow_origins=["https://app1.example.com", "https://app2.example.com"], allow_methods=["*"], allow_headers=["*"])
B. app.add_middleware(CORSMiddleware, allow_origins=["*"])
C. app.add_middleware(CORSMiddleware, allow_origins=["https://app1.example.com", "https://app2.example.com"], allow_methods=["GET", "POST"], allow_headers=["Content-Type"])
D. app.add_middleware(CORSMiddleware, allow_origins="https://app1.example.com,https://app2.example.com", allow_methods=["*"], allow_headers=["*"])
Solution
Step 1: Set allow_origins correctly
To allow two specific domains, use a list with both URLs as strings.
Step 2: Allow all methods and headers
Using ["*"] for allow_methods and allow_headers allows all HTTP methods and headers.
Step 3: Check for syntax correctness
app.add_middleware(CORSMiddleware, allow_origins=["https://app1.example.com", "https://app2.example.com"], allow_methods=["*"], allow_headers=["*"]) correctly uses a list for origins and lists with "*" for methods and headers.
Final Answer:
app.add_middleware(CORSMiddleware, allow_origins=["https://app1.example.com", "https://app2.example.com"], allow_methods=["*"], allow_headers=["*"]) -> Option A
Quick Check:
List origins + wildcard methods/headers [OK]
Hint: Use list for origins and ["*"] to allow all methods/headers [OK]
Common Mistakes:
Passing origins as a single comma string
Using allow_methods with limited verbs instead of wildcard
Setting allow_origins to ["*"] when only specific domains needed