Bird
0
0

Given this FastAPI endpoint, what will be the output if two files named 'file1.txt' and 'file2.txt' are uploaded?

medium📝 component behavior Q4 of 15
FastAPI - File Handling
Given this FastAPI endpoint, what will be the output if two files named 'file1.txt' and 'file2.txt' are uploaded?
from fastapi import FastAPI, UploadFile, File
from typing import List

app = FastAPI()

@app.post('/upload')
async def upload(files: List[UploadFile] = File(...)):
    return {'filenames': [file.filename for file in files]}
ARuntime error due to wrong type
B{'filenames': ['files']}
C{'filenames': ['file1.txt', 'file2.txt']}
D{'filenames': []}
Step-by-Step Solution
Solution:
  1. Step 1: Understand the endpoint behavior

    The endpoint returns a dictionary with filenames extracted from the uploaded files list.
  2. Step 2: Predict output for two uploaded files

    Uploading 'file1.txt' and 'file2.txt' will produce a list of their filenames in the response.
  3. Final Answer:

    {'filenames': ['file1.txt', 'file2.txt']} -> Option C
  4. Quick Check:

    Uploaded files list returns filenames correctly [OK]
Quick Trick: List comprehension extracts filenames from UploadFile list [OK]
Common Mistakes:
MISTAKES
  • Expecting filenames as a single string
  • Confusing parameter name with filenames
  • Assuming empty list if files uploaded

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes