Bird
0
0

Consider this FastAPI endpoint:

medium📝 component behavior Q5 of 15
FastAPI - File Handling
Consider this FastAPI endpoint:
async def upload(file: UploadFile = File(...)):
    return {"name": file.filename}

What will be the JSON response if a file named 'photo.jpg' is uploaded?
A{"filename": "photo.jpg"}
B{"name": "photo.jpg"}
C{"file": "photo.jpg"}
D{"name": "file"}
Step-by-Step Solution
Solution:
  1. Step 1: Check the return dictionary keys

    The endpoint returns a dictionary with key "name" and value file.filename.
  2. Step 2: Understand file.filename

    file.filename contains the uploaded file's original filename, here 'photo.jpg'.
  3. Final Answer:

    {"name": "photo.jpg"} -> Option B
  4. Quick Check:

    Returned key matches 'name' exactly [OK]
Quick Trick: Return keys must match exactly as coded [OK]
Common Mistakes:
MISTAKES
  • Assuming key is 'filename' instead of 'name'
  • Confusing file content with filename
  • Returning file object instead of filename string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes