Bird
0
0

Identify the error in this FastAPI endpoint for file download:

medium📝 Debug Q14 of 15
FastAPI - File Handling
Identify the error in this FastAPI endpoint for file download:
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/getfile')
def get_file():
    return FileResponse(path='myfile.txt', media_type='text/plain', filename=myfile.txt)
AFileResponse must be awaited since endpoint is async
BFilename argument is not a string (missing quotes)
CPath argument should be a URL, not a file path
DMissing import for FileResponse
Step-by-Step Solution
Solution:
  1. Step 1: Check filename argument syntax

    The filename argument is written as filename=myfile.txt without quotes, so Python treats it as a variable, causing a NameError.
  2. Step 2: Verify other parts

    The endpoint is synchronous which is allowed. Path can be a file path. FileResponse is imported correctly. So only filename syntax is wrong.
  3. Final Answer:

    Filename argument is not a string (missing quotes) -> Option B
  4. Quick Check:

    Filename must be a string literal [OK]
Quick Trick: Always quote filename strings in FileResponse [OK]
Common Mistakes:
MISTAKES
  • Forgetting quotes around filename string
  • Assuming async needed for FileResponse
  • Confusing file path with URL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes