0
0
FastAPIframework~10 mins

File download responses 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 FastAPI class for creating the app.

FastAPI
from fastapi import [1]
app = [1]()
Drag options to blanks, or click blank then click option'
AFileResponse
BRequest
CFastAPI
DResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Response instead of FastAPI
Using FileResponse to create the app
2fill in blank
medium

Complete the code to import the correct class for sending files in FastAPI.

FastAPI
from fastapi.responses import [1]
Drag options to blanks, or click blank then click option'
ARedirectResponse
BFileResponse
CHTMLResponse
DJSONResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSONResponse for file downloads
Confusing FileResponse with RedirectResponse
3fill in blank
hard

Fix the error in the route to correctly return a file download response.

FastAPI
@app.get('/download')
async def download_file():
    return [1]('example.txt')
Drag options to blanks, or click blank then click option'
AFileResponse
BResponse
CJSONResponse
DHTMLResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Returning Response instead of FileResponse
Returning JSONResponse for file downloads
4fill in blank
hard

Fill both blanks to set a custom filename and media type for the file download.

FastAPI
@app.get('/download')
async def download_file():
    return FileResponse('example.txt', filename=[1], media_type=[2])
Drag options to blanks, or click blank then click option'
A'myfile.txt'
B'application/pdf'
C'text/plain'
D'download.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using media_type as filename
Setting media_type to an incorrect MIME type
5fill in blank
hard

Fill all three blanks to add a custom header and use FileResponse correctly.

FastAPI
@app.get('/download')
async def download_file():
    headers = [1]
    return FileResponse('example.txt', filename=[2], headers=[3])
Drag options to blanks, or click blank then click option'
A{'X-Custom-Header': 'value'}
B'report.pdf'
Cheaders
D'example.txt'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing headers as a string instead of dict
Not passing the headers argument to FileResponse