Bird
0
0

How can you customize HTTP headers like Cache-Control when returning a file with FileResponse in FastAPI?

hard🚀 Application Q9 of 15
FastAPI - File Handling
How can you customize HTTP headers like Cache-Control when returning a file with FileResponse in FastAPI?
APass a dictionary with headers to the 'headers' parameter of FileResponse.
BModify the global FastAPI app headers before returning FileResponse.
CSet headers inside the file content before sending.
DUse a middleware to add headers after FileResponse is returned.
Step-by-Step Solution
Solution:
  1. Step 1: Check FileResponse parameters for headers

    FileResponse accepts a 'headers' parameter where you can pass a dictionary of HTTP headers.
  2. Step 2: Understand other options

    Modifying global app headers or file content does not affect response headers directly. Middleware can add headers but is more complex.
  3. Final Answer:

    Pass a dictionary with headers to the 'headers' parameter of FileResponse. -> Option A
  4. Quick Check:

    Use headers param in FileResponse for custom headers [OK]
Quick Trick: Use headers param dict to add HTTP headers in FileResponse [OK]
Common Mistakes:
MISTAKES
  • Trying to set headers inside file content
  • Changing global app headers instead of response headers
  • Assuming middleware is required for simple header changes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes