Complete the code to import the correct class for sending a file response in FastAPI.
from fastapi import FastAPI from fastapi.responses import [1] app = FastAPI()
The FileResponse class is used in FastAPI to send files as responses.
Complete the code to return a file named 'example.txt' from the '/download' route.
from fastapi import FastAPI from fastapi.responses import FileResponse app = FastAPI() @app.get('/download') async def download_file(): return [1]('example.txt')
Use FileResponse to send the file 'example.txt' as a response.
Fix the error in the code to correctly send a file with a custom filename 'report.pdf' in the response.
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')
The correct parameter to set the download filename in FileResponse is filename.
Fill both blanks to send a file with media type 'application/pdf' and enable HTTP caching.
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')
The media_type should be 'application/pdf' for PDF files, and cache_control enables HTTP caching.
Fill all three blanks to send a file with custom filename, media type, and set background task to delete the file after sending.
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))
Set filename to 'data.txt', media_type to 'text/plain', and pass the function remove_file (without calling it) to BackgroundTask to delete the file after sending.