0
0
FastAPIframework~10 mins

CORS middleware setup in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the CORS middleware class from FastAPI.

FastAPI
from fastapi.middleware.[1] import CORSMiddleware
Drag options to blanks, or click blank then click option'
Acors
BCORS
CCors
DCorsMiddleware
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'CORS' instead of lowercase 'cors'.
Trying to import from 'fastapi.middleware.corsmiddleware'.
2fill in blank
medium

Complete the code to add the CORS middleware to the FastAPI app allowing all origins.

FastAPI
app.add_middleware(CORSMiddleware, allow_origins=[1], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
Drag options to blanks, or click blank then click option'
A['*']
B['http://localhost']
C['any']
D['all']
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings like 'all' or 'any' instead of '*' wildcard.
Passing a single string instead of a list.
3fill in blank
hard

Fix the error in the CORS middleware setup by completing the missing argument for allowed methods.

FastAPI
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=[1], allow_headers=["*"])
Drag options to blanks, or click blank then click option'
A"GET"
B["GET", "POST"]
C"*"
D["*"]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string instead of a list for allow_methods.
Using only one method string without list brackets.
4fill in blank
hard

Fill both blanks to create a FastAPI app and add CORS middleware allowing only specific origins and methods.

FastAPI
app = [1]()
app.add_middleware(CORSMiddleware, allow_origins=[2], allow_credentials=True, allow_methods=["GET", "POST"], allow_headers=["*"])
Drag options to blanks, or click blank then click option'
AFastAPI
B['http://example.com']
C['*']
DStarlette
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Starlette' instead of 'FastAPI' to create the app.
Using wildcard '*' when only specific origins are allowed.
5fill in blank
hard

Fill all three blanks to configure CORS middleware with specific origins, methods, and headers.

FastAPI
app.add_middleware(CORSMiddleware, allow_origins=[1], allow_methods=[2], allow_headers=[3], allow_credentials=True)
Drag options to blanks, or click blank then click option'
A['https://site1.com', 'https://site2.com']
B["GET", "POST", "PUT"]
C["Content-Type", "Authorization"]
D['*']
Attempts:
3 left
💡 Hint
Common Mistakes
Using wildcard '*' for all fields when specific values are required.
Passing strings instead of lists for methods or headers.