Bird
0
0

Consider the following FastAPI code snippet configuring CORS middleware:

medium📝 component behavior Q4 of 15
FastAPI - Middleware and Hooks
Consider the following FastAPI code snippet configuring CORS middleware:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["https://myfrontend.com"],
    allow_methods=["PUT", "DELETE"],
    allow_headers=["Authorization"]
)

What will be the effect of this CORS configuration on incoming requests?
AOnly requests from https://myfrontend.com using PUT or DELETE methods with Authorization header will be allowed.
BAll origins can send PUT and DELETE requests with any headers.
CRequests from any origin using GET or POST methods will be allowed.
DRequests from https://myfrontend.com with any HTTP method and headers will be allowed.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze allow_origins

    Only requests from "https://myfrontend.com" are allowed.
  2. Step 2: Analyze allow_methods

    Only PUT and DELETE HTTP methods are permitted.
  3. Step 3: Analyze allow_headers

    Only requests with the "Authorization" header are allowed.
  4. Final Answer:

    Only requests from https://myfrontend.com using PUT or DELETE methods with Authorization header will be allowed. -> Option A
  5. Quick Check:

    Check origins, methods, and headers match exactly. [OK]
Quick Trick: Match origins, methods, and headers exactly to allow requests. [OK]
Common Mistakes:
MISTAKES
  • Assuming all methods are allowed when only specific ones are set.
  • Using a string instead of a list for allow_methods.
  • Believing allow_headers=['Authorization'] allows all headers.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes