0
0
FastAPIframework~20 mins

File upload (single file) in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FastAPI File Upload Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a single file is uploaded using FastAPI's UploadFile?

Consider this FastAPI endpoint that accepts a single file upload:

from fastapi import FastAPI, File, UploadFile

app = FastAPI()

@app.post('/upload')
async def upload_file(file: UploadFile = File(...)):
    content = await file.read()
    return {'filename': file.filename, 'size': len(content)}

What will be the output if a file named example.txt with 100 bytes is uploaded?

FastAPI
from fastapi import FastAPI, File, UploadFile

app = FastAPI()

@app.post('/upload')
async def upload_file(file: UploadFile = File(...)):
    content = await file.read()
    return {'filename': file.filename, 'size': len(content)}
A{"filename": "example.txt", "size": 100}
B{"filename": "file", "size": 100}
C{"filename": "example.txt", "size": 0}
DTypeError: object of type 'UploadFile' has no len()
Attempts:
2 left
💡 Hint

Remember that file.filename gives the original file name and await file.read() reads the file content bytes.

📝 Syntax
intermediate
2:00remaining
Which option correctly declares a FastAPI endpoint to accept a single file upload?

Choose the correct way to declare a FastAPI POST endpoint that accepts a single file upload using UploadFile and File.

A
@app.post('/upload')
async def upload(file: UploadFile):
    return {'filename': file.filename}
B
@app.post('/upload')
def upload(file: UploadFile = File(...)):
    return {'filename': file.filename}
C
@app.post('/upload')
async def upload(file: bytes = File(...)):
    return {'filename': file.filename}
D
@app.post('/upload')
async def upload(file: UploadFile = File(...)):
    return {'filename': file.filename}
Attempts:
2 left
💡 Hint

Remember that file uploads should be declared with UploadFile and File(...) to mark the parameter as required.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI file upload endpoint raise an error?

Given this FastAPI endpoint:

@app.post('/upload')
async def upload(file: UploadFile = File(...)):
    content = file.read()
    return {'size': len(content)}

What error will occur when uploading a file and why?

FastAPI
@app.post('/upload')
async def upload(file: UploadFile = File(...)):
    content = file.read()
    return {'size': len(content)}
ANo error, returns file size correctly
BTypeError: object of type 'UploadFile' has no attribute 'read'
CRuntimeWarning: coroutine 'UploadFile.read' was never awaited
DValueError: file parameter missing
Attempts:
2 left
💡 Hint

Check if file.read() is awaited properly in async functions.

state_output
advanced
2:00remaining
What is the value of 'file_content' after this FastAPI upload endpoint runs?

Consider this FastAPI endpoint:

@app.post('/upload')
async def upload(file: UploadFile = File(...)):
    file_content = await file.read()
    await file.seek(0)
    second_read = await file.read()
    return {'first_read_size': len(file_content), 'second_read_size': len(second_read)}

What will be the returned JSON if the uploaded file has 50 bytes?

FastAPI
@app.post('/upload')
async def upload(file: UploadFile = File(...)):
    file_content = await file.read()
    await file.seek(0)
    second_read = await file.read()
    return {'first_read_size': len(file_content), 'second_read_size': len(second_read)}
ARuntimeError: seek() not supported
B{"first_read_size": 50, "second_read_size": 50}
C{"first_read_size": 0, "second_read_size": 50}
D{"first_read_size": 50, "second_read_size": 0}
Attempts:
2 left
💡 Hint

Think about what seek(0) does to the file pointer before the second read.

🧠 Conceptual
expert
2:00remaining
Which statement about FastAPI's UploadFile is TRUE?

Choose the correct statement about UploadFile in FastAPI.

A<code>UploadFile</code> provides a file-like async interface and stores the file on disk or in memory depending on size.
B<code>UploadFile</code> stores the entire file content in memory as bytes immediately upon upload.
C<code>UploadFile</code> can only be used synchronously and does not support async methods.
D<code>UploadFile</code> automatically saves the uploaded file permanently to a fixed server directory.
Attempts:
2 left
💡 Hint

Consider how UploadFile manages file data and async support.