How to make a large file accessible to external APIs?

Returning a File Response First, to return a file that is saved on disk from a FastAPI backend, you could use FileResponse (in case the file was already fully loaded into memory, see here). For example: from fastapi import FastAPI from fastapi.responses import FileResponse some_file_path = “large-video-file.mp4” app = FastAPI() @app.get(“/”) def main(): return FileResponse(some_file_path) … Read more

FastAPI’s RedirectResponse doesn’t work as expected in Swagger UI

To start with, the HTTP OPTIONS, in CORS, is a preflight request that is automatically issued by the browser, before the actual request—is not the one that returns the File response. It requests the permitted communication options for a given server, and the server responds with an Access-Control-Allow-Methods header including a set of permitted methods … Read more

How to pass URL as a path parameter to a FastAPI route?

Option 1 You could simply use Starlette’s path convertor to capture arbitrary paths. As per Starlette documentation, path returns the rest of the path, including any additional / characters. from fastapi import Request @app.get(‘/{_:path}’) def pred_image(request: Request): return {‘path’: request.url.path[1:]} or @app.get(‘/{full_path:path}’) def pred_image(full_path: str): return {‘path’: full_path} Test using the link below: http://127.0.0.1:8000/https://raw.githubusercontent.com/ultralytics/yolov5/master/data/images/zidane.jpg Please … Read more

How to define multiple API endpoints in FastAPI with different paths but the same path parameter?

In FastAPI, as described in this answer, because endpoints are evaluated in order (see FastAPI’s about how order matters), it makes sure that the endpoint you defined first in your app—in this case, that is, /project/{project_id}/…—will be evaluated first. Hence, every time you call one of the other two endpoints, i.e., /project/details/… and /project/metadata/…, the … Read more

How to upload a CSV file in FastAPI and convert it into JSON?

Below are given various options on how to convert the uploaded .csv file into JSON. The following .csv sample file is used in the examples below. data.csv Id,name,age,height,weight 1,Alice,20,62,120.6 2,Freddie,21,74,190.6 3,Bob,17,68,120.0 Option 1 The csv.DictReader() method can accept as a file argument file objects as well. FastAPI’s UploadFile uses Python’s SpooledTemporaryFile, a file-like object (for … Read more

How to customise error response for a specific route in FastAPI?

Option 1 If you didn’t mind having the Header showing as Optional in OpenAPI/Swagger UI autodocs, it would be as easy as follows: from fastapi import Header, HTTPException @app.post(“/”) def some_route(some_custom_header: Optional[str] = Header(None)): if not some_custom_header: raise HTTPException(status_code=401, detail=”Unauthorized”) return {“some-custom-header”: some_custom_header} Option 2 However, since you would like the Header to appear as … Read more