CORS — Letting Browsers Call Your API — Concept Check
Questions on CORS — Letting Browsers Call Your API. Browsers block cross-origin responses unless your API opts in via CORS headers — CORSMiddleware with an explicit origin list, allow_credentials rules, and the knowledge that CORS is browser policy, not API security.
Questions in this quiz
8 questions on CORS — Letting Browsers Call Your API — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.Your Next.js app on localhost:3000 calls your FastAPI on localhost:8000 and receives a CORS error. What is the most likely cause?
- AThe API server is down.
- BThe browser is blocking all API requests by default.
- CThe Access-Control-Allow-Origin header is missing or incorrect.
- DThe API endpoint is not defined.
2.A developer sets allow_origins=["*"] and allow_credentials=True in CORSMiddleware. What happens when a browser makes a request with credentials?
- AThe request is blocked only if the API server is down.
- BThe request fails because the combination is forbidden by the CORS spec.
- CThe request succeeds only if the browser supports CORS.
- DThe request succeeds because the wildcard allows all origins.
3.Your FastAPI server is configured with CORSMiddleware but a client reports a CORS error when sending a POST request with an Authorization header. What should you check first?
- AVerify the API endpoint is correct.
- BCheck if the API server is running.
- CEnsure the OPTIONS preflight request is not blocked by any middleware.
- DEnsure the client is sending the correct data format.
4.Which of the following statements is true about CORS?
- ACORS prevents all cross-origin requests by default.
- BCORS is enforced by the server, not the browser.
- CCORS ensures API security by blocking unauthorized requests.
- DCORS protects browser users from unauthorized data access.
5.A developer configures CORSMiddleware with allow_origins=["http://localhost:3000"]. Why might requests from "http://localhost:3000/" still fail?
- AThe CORS configuration is missing allow_methods.
- BThe browser does not support CORS for localhost.
- CThe trailing slash causes the origin match to fail.
- DThe API server is not configured to handle requests.
6.Your FastAPI server allows all methods and headers in CORSMiddleware. What should you ensure when deploying to production?
- AThe allowed origins are explicitly listed and not set to "*".
- BThe API endpoints are documented.
- CThe server logs all incoming requests.
- DThe server is running on a secure port.
7.A developer notices that their API works with curl but fails in the browser due to CORS. What is a likely reason?
- AThe server is not responding to requests.
- BThe browser does not support the requested method.
- CThe API endpoint is incorrect.
- DCORS is a browser policy and does not affect tools like curl.
8.What happens if a browser sends a request with a custom header not listed in allow_headers of CORSMiddleware?
- AThe request is blocked by the API server.
- BThe request triggers a preflight check and fails if the header is not allowed.
- CThe request succeeds without any issues.
- DThe request is automatically retried without the custom header.