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.
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.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.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.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.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.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.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.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.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