0
0
FastAPIframework~10 mins

Multiple file uploads in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the correct class for handling file uploads in FastAPI.

FastAPI
from fastapi import FastAPI, [1]
Drag options to blanks, or click blank then click option'
AUploadFile
BRequest
CFile
DForm
Attempts:
3 left
💡 Hint
Common Mistakes
Using File instead of UploadFile
Importing Form instead of UploadFile
2fill in blank
medium

Complete the code to accept multiple files in the endpoint function parameter.

FastAPI
async def upload_files(files: list[[1]]):
Drag options to blanks, or click blank then click option'
Abytes
BFile
CUploadFile
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using list[File] instead of list[UploadFile]
Using list[str] which is incorrect for files
3fill in blank
hard

Fix the error in the endpoint decorator to accept multiple files with the correct parameter.

FastAPI
@app.post("/upload")
async def upload(files: list[UploadFile] = [1]):
Drag options to blanks, or click blank then click option'
AFile()
BFile(...)
CForm(...)
DDepends()
Attempts:
3 left
💡 Hint
Common Mistakes
Using File() which makes the parameter optional
Using Form(...) which is for form fields, not files
4fill in blank
hard

Fill both blanks to read the filename and content of each uploaded file.

FastAPI
for file in files:
    filename = file.[1]
    content = await file.[2]()
Drag options to blanks, or click blank then click option'
Afilename
Bread
Ccontent
Dwrite
Attempts:
3 left
💡 Hint
Common Mistakes
Using file.content instead of file.filename
Calling file.write() instead of file.read()
5fill in blank
hard

Fill all three blanks to define the FastAPI app, import UploadFile and File, and create the upload endpoint.

FastAPI
from fastapi import [1], UploadFile, File

app = [2]()

@app.post("/upload")
async def upload(files: list[UploadFile] = File(...)):
    return {file.filename: await file.read() for file in [3]
Drag options to blanks, or click blank then click option'
AFastAPI
Bapp
Cfiles
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'app' instead of 'FastAPI' in import
Using 'app' instead of 'FastAPI()' to create the app
Using wrong variable name instead of 'files' in the comprehension