Bird
0
0
FastAPIframework~10 mins

Why file operations are common in FastAPI - Test Your Understanding

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

Complete the code to import the FastAPI class.

FastAPI
from fastapi import [1]
app = [1]()
Drag options to blanks, or click blank then click option'
AFastAPI
BRequest
CResponse
DFile
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request or Response instead of FastAPI.
Trying to import File here.
2fill in blank
medium

Complete the code to define a POST endpoint that accepts a file upload.

FastAPI
@app.post("/upload")
async def upload_file(file: [1]):
    content = await file.read()
    return {"size": len(content)}
Drag options to blanks, or click blank then click option'
Astr
BUploadFile
CFile
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using File instead of UploadFile as the parameter type.
Using str or int which are not file types.
3fill in blank
hard

Fix the error in the import statement to handle file uploads.

FastAPI
from fastapi import [1]
from fastapi import UploadFile, File
Drag options to blanks, or click blank then click option'
ARequest
BFile
CFastAPI
DDepends
Attempts:
3 left
💡 Hint
Common Mistakes
Importing File twice or missing FastAPI import.
Confusing Request with FastAPI.
4fill in blank
hard

Fill both blanks to save the uploaded file content to disk.

FastAPI
async def save_file(file: UploadFile):
    content = await file.[1]()
    with open("uploaded_file", [2]) as f:
        f.write(content)
Drag options to blanks, or click blank then click option'
Aread
Bwrite
C"wb"
D"r"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'write' method on UploadFile which does not exist.
Opening file in read mode instead of write mode.
5fill in blank
hard

Fill all three blanks to create a FastAPI endpoint that saves an uploaded file and returns its size.

FastAPI
@app.post("/save")
async def save_upload(file: [1] = [2](...)):
    content = await file.[3]()
    with open("saved_file", "wb") as f:
        f.write(content)
    return {"size": len(content)}
Drag options to blanks, or click blank then click option'
AUploadFile
BFile
Cread
Dstr
Attempts:
3 left
💡 Hint
Common Mistakes
Using str instead of UploadFile for file parameter.
Missing File() dependency declaration.
Using write() instead of read() on UploadFile.