Bird
0
0

Find the bug in this FastAPI file download code:

medium📝 Debug Q7 of 15
FastAPI - File Handling
Find the bug in this FastAPI file download code:
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/file')
async def file():
    return FileResponse('static/data.csv', filename='data.csv', content_type='text/csv')
AThe file path must be absolute, not relative.
BThe parameter 'content_type' should be 'media_type'.
CThe endpoint function cannot be async when returning FileResponse.
DThe filename parameter is missing.
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter names for FileResponse

    The correct parameter for MIME type is 'media_type', not 'content_type'.
  2. Step 2: Validate other parts

    Relative paths are allowed, async functions can return FileResponse, and filename is provided.
  3. Final Answer:

    The parameter 'content_type' should be 'media_type'. -> Option B
  4. Quick Check:

    Use media_type, not content_type, in FileResponse [OK]
Quick Trick: FileResponse uses media_type, not content_type [OK]
Common Mistakes:
MISTAKES
  • Using content_type instead of media_type
  • Thinking async is disallowed for FileResponse
  • Believing file path must be absolute

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes