QuizzesHeaders, Cookies & the Request Object — Concept Check
intermediate
fastapi

Headers, Cookies & the Request Object — Concept Check

Questions on Headers, Cookies & the Request Object. Header() and Cookie() bind request metadata as typed parameters (X-API-Key becomes x_api_key); Response.set_cookie sends session cookies with the security flags that interviews love; Request is the escape hatch.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Headers, Cookies & the Request Object — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.A developer uses FastAPI to read headers and cookies. They define a parameter as `x_api_key: str = Header()`. What happens if the client does not provide this header?

    • AThe request fails with a 401 error
    • BThe request fails with a 422 error
    • CThe request proceeds without the header
    • DThe request proceeds with a default value
  2. 2.A FastAPI application sets a cookie with `response.set_cookie(key='session_id', httponly=True, secure=True, samesite='lax')`. What is the main purpose of these flags?

    • ATo make cookies available across all sites
    • BTo ensure cookies are always sent over HTTP
    • CTo enhance security against XSS and CSRF attacks
    • DTo allow JavaScript to access cookies
  3. 3.A developer notices that `request.client.host` returns the proxy's IP instead of the client's IP. What should they check to get the real client IP?

    • ACheck the `Content-Type` header
    • BCheck the `Referer` header
    • CCheck the `X-Forwarded-For` header
    • DCheck the `User-Agent` header
  4. 4.In a FastAPI application, what happens if you provide a default value to a `Header()` parameter?

    • AThe header is required
    • BThe header is ignored
    • CThe header is always converted to lowercase
    • DThe header becomes optional
  5. 5.A developer uses `request.headers.get('x-forwarded-for')` to log client IPs. Why might this be insecure?

    • AThe `X-Forwarded-For` header can be spoofed
    • BIt logs sensitive user data
    • CIt requires HTTPS to be secure
    • DIt exposes the server's internal IP
  6. 6.What is the primary function of the `Request` object in FastAPI?

    • ATo automatically validate headers
    • BTo access raw request data like URL and client IP
    • CTo send cookies with security flags
    • DTo convert underscores to hyphens in headers
  7. 7.A developer sets a cookie without the `secure` flag. What risk does this introduce?

    • AThe cookie can be intercepted over HTTP
    • BThe cookie is accessible via JavaScript
    • CThe cookie is not sent with cross-site requests
    • DThe cookie expires immediately
  8. 8.In FastAPI, how are underscores in header parameter names treated?

    • AThey cause an error
    • BThey are automatically converted to hyphens
    • CThey are removed
    • DThey are left unchanged