0
0
FastAPIframework~20 mins

File download responses in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Download Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI file download endpoint?
Consider this FastAPI endpoint code. What will the client receive when accessing /download?
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='example.txt')
AThe server raises a FileNotFoundError because 'example.txt' is missing.
BThe client downloads the file 'example.txt' with correct text content and filename.
CThe client receives a JSON response with file metadata instead of the file content.
DThe client receives an HTML page showing the file content inline.
Attempts:
2 left
💡 Hint
FileResponse sends the file content with headers to trigger download.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets a custom filename for download in FastAPI?
You want the client to download a file named 'report.pdf' regardless of the actual file name on disk. Which code snippet achieves this?
FastAPI
from fastapi.responses import FileResponse

# Assume 'files/12345.pdf' exists

@app.get('/get-report')
async def get_report():
    # Fill in the return statement
Areturn FileResponse('files/12345.pdf', filename='report.pdf')
Breturn FileResponse('files/12345.pdf', media_type='application/pdf')
Creturn FileResponse('files/12345.pdf', headers={'Content-Disposition': 'attachment; filename=report.pdf'})
Dreturn FileResponse('files/12345.pdf', filename='files/12345.pdf')
Attempts:
2 left
💡 Hint
Use the filename parameter to set the download name.
🔧 Debug
advanced
2:00remaining
Why does this FastAPI file download endpoint cause a 500 Internal Server Error?
Analyze the code below. Why does the server return a 500 error when the endpoint is called?
FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/download')
async def download():
    file_path = 'static/report.pdf'
    return FileResponse(path=file_path, media_type='application/pdf', filename='report.pdf')
AThe file 'static/report.pdf' does not exist or path is incorrect, causing FileNotFoundError.
BThe media_type 'application/pdf' is invalid and causes a runtime error.
CThe filename parameter must be omitted to avoid errors.
DThe endpoint must be synchronous, async causes the error.
Attempts:
2 left
💡 Hint
Check if the file path is correct and file exists on the server.
state_output
advanced
2:00remaining
What headers are set by FastAPI's FileResponse for file downloads?
Given this endpoint, which HTTP header ensures the browser downloads the file instead of displaying it inline?
FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/file')
async def file():
    return FileResponse('data.csv', media_type='text/csv', filename='data.csv')
ACache-Control: no-cache
BContent-Type: application/octet-stream
CContent-Encoding: gzip
DContent-Disposition: attachment; filename="data.csv"
Attempts:
2 left
💡 Hint
Look for the header that controls download behavior in browsers.
🧠 Conceptual
expert
3:00remaining
How does FastAPI's FileResponse handle large files efficiently?
FastAPI's FileResponse is used to send large files to clients. How does it avoid loading the entire file into memory at once?
AIt compresses the file fully before sending to reduce memory usage.
BIt reads the entire file into memory and then sends it in one response.
CIt streams the file in chunks using Python's file iterator, sending data progressively.
DIt converts the file to base64 string and sends it as JSON.
Attempts:
2 left
💡 Hint
Think about how servers send big files without freezing or crashing.