Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Response instead of FastAPI
Using FileResponse to create the app
✗ Incorrect
The FastAPI class is used to create the app instance.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using JSONResponse for file downloads
Confusing FileResponse with RedirectResponse
✗ Incorrect
FileResponse is the class used to send files as responses in FastAPI.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning Response instead of FileResponse
Returning JSONResponse for file downloads
✗ Incorrect
FileResponse must be used to return a file for download.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using media_type as filename
Setting media_type to an incorrect MIME type
✗ Incorrect
The filename parameter sets the download name, media_type sets the file type.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing headers as a string instead of dict
Not passing the headers argument to FileResponse
✗ Incorrect
Headers must be a dictionary, filename a string, and headers passed as the headers argument.