0
0
FastAPIframework~10 mins

File 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 class for sending a file response in FastAPI.

FastAPI
from fastapi import FastAPI
from fastapi.responses import [1]

app = FastAPI()
Drag options to blanks, or click blank then click option'
AHTMLResponse
BJSONResponse
CRedirectResponse
DFileResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Importing JSONResponse or HTMLResponse instead of FileResponse.
Forgetting to import any response class.
2fill in blank
medium

Complete the code to return a file named 'example.txt' from the '/download' route.

FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/download')
async def download_file():
    return [1]('example.txt')
Drag options to blanks, or click blank then click option'
AJSONResponse
BHTMLResponse
CFileResponse
DRedirectResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Returning JSONResponse or HTMLResponse instead of FileResponse.
Forgetting to specify the filename.
3fill in blank
hard

Fix the error in the code to correctly send a file with a custom filename 'report.pdf' in the response.

FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/report')
async def get_report():
    return FileResponse('files/report.pdf', [1]='report.pdf')
Drag options to blanks, or click blank then click option'
Afile
Bfilename
Cname
Dfile_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'file_name' or 'name' instead of 'filename'.
Passing the filename as a positional argument.
4fill in blank
hard

Fill both blanks to send a file with media type 'application/pdf' and enable HTTP caching.

FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse

app = FastAPI()

@app.get('/pdf')
async def get_pdf():
    return FileResponse('docs/sample.pdf', media_type=[1], [2]='max-age=3600')
Drag options to blanks, or click blank then click option'
A'application/pdf'
Bbackground
Ccache_control
Duse_cache
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong media type strings.
Using incorrect parameter names for caching.
5fill in blank
hard

Fill all three blanks to send a file with custom filename, media type, and set background task to delete the file after sending.

FastAPI
from fastapi import FastAPI
from fastapi.responses import FileResponse
from fastapi.background import BackgroundTask
import os

app = FastAPI()

def remove_file(path: str):
    os.remove(path)

@app.get('/tempfile')
async def send_temp_file():
    path = 'temp/data.txt'
    return FileResponse(path, filename=[1], media_type=[2], background=BackgroundTask([3], path))
Drag options to blanks, or click blank then click option'
A'data.txt'
B'text/plain'
Cremove_file(path)
Dremove_file
Attempts:
3 left
💡 Hint
Common Mistakes
Calling remove_file(path) instead of passing remove_file.
Using wrong media type strings.
Not setting the filename parameter.