0
0
FastAPIframework~20 mins

File responses in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this FastAPI endpoint return?
Consider this FastAPI endpoint that uses FileResponse to send a file to the client. What will the client receive when accessing this endpoint?
FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/download')
async def download_file():
    return FileResponse('example.txt', media_type='text/plain', filename='downloaded.txt')
AThe client downloads the file 'example.txt' but it is saved as 'downloaded.txt' on the client side.
BThe client gets a JSON response describing the file 'example.txt'.
CThe client receives the content of 'example.txt' displayed as plain text in the browser.
DThe client receives a 404 error because FileResponse cannot find 'example.txt'.
Attempts:
2 left
💡 Hint
Think about what the filename parameter does in FileResponse.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this FastAPI file response code
Which option contains a syntax error that will prevent the FastAPI app from running?
FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/file')
async def get_file():
    return FileResponse('data.csv', media_type='text/csv', filename='data.csv')
Areturn FileResponse('data.csv')
Breturn FileResponse('data.csv', media_type='text/csv', filename='data.csv')
Creturn FileResponse('data.csv', media_type='text/csv')
Dreturn FileResponse('data.csv', media_type='text/csv' filename='data.csv')
Attempts:
2 left
💡 Hint
Look carefully at the commas between arguments.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI FileResponse raise a FileNotFoundError?
This FastAPI endpoint raises a FileNotFoundError when accessed. What is the most likely cause?
FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/getfile')
async def get_file():
    return FileResponse('files/report.pdf')
AThe file 'files/report.pdf' does not exist at the specified path.
BFileResponse requires an absolute path, relative paths cause FileNotFoundError.
CThe media_type parameter is missing, causing the error.
DFastAPI does not support serving PDF files with FileResponse.
Attempts:
2 left
💡 Hint
Check if the file path is correct and the file exists.
state_output
advanced
2:00remaining
What is the Content-Disposition header value sent by this FileResponse?
Given this FastAPI endpoint, what will be the exact Content-Disposition header value in the HTTP response?
FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/download')
async def download():
    return FileResponse('image.png', filename='picture.png')
Ainline; filename=image.png
Battachment; filename=picture.png
Cattachment; filename=image.png
Dinline; filename=picture.png
Attempts:
2 left
💡 Hint
FileResponse sets Content-Disposition to attachment by default when filename is provided.
🧠 Conceptual
expert
3:00remaining
Which option correctly streams a large file efficiently with FastAPI?
You want to serve a very large file without loading it fully into memory. Which FastAPI FileResponse usage is best for this?
Areturn FileResponse('largefile.zip')
Breturn FileResponse(open('largefile.zip', 'rb'))
Creturn FileResponse('largefile.zip', media_type='application/zip', filename='largefile.zip')
Dreturn FileResponse('largefile.zip', chunk_size=1024*1024)
Attempts:
2 left
💡 Hint
Consider how FileResponse handles file streaming and metadata.