FastAPI serve tar file example one-liner code
Refined the query to include 'example' and 'code' for more specific results, ensuring it focuses on practical implementations of serving a tar file with FastAPI.
To serve a .tar file using FastAPI in a single line, you can utilize the FileResponse class from FastAPI. Here's a concise example demonstrating how to achieve this:
from fastapi import FastAPI
from fastapi.responses import FileResponse
app = FastAPI()
@app.get("/download")
def download_tar():
return FileResponse("path/to/your/file.tar")
In this example, replace "path/to/your/file.tar" with the actual file path of the .tar file you want to serve. When a request is made to the /download endpoint, the specified tar file will be sent as a response, allowing users to download it easily. For more details on serving files in FastAPI, you may refer to the FastAPI documentation on Static Files.