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 process requests from multiiple users using ML model and FastAPI?

First, you should rather not load your model every time a request arrives, but rahter have it loaded once at startup (you could use the startup event for this) and store it on the app instance—using the generic app.state attribute (see implementation of State too)—which you can later retrieve, as described here and here. For … Read more

How to return and download Excel file using FastAPI?

You could set the Content-Disposition header using the attachment parameter, indicating to the browser that the file should be downloaded, as described in the answers here and here. Swagger UI will provide a Download file link for you to download the file, as soon as you execute the request. headers = {‘Content-Disposition’: ‘attachment; filename=”Book.xlsx”‘} return … Read more

How to download a file using ReactJS with Axios in the frontend and FastAPI in the backend?

In the Axios GET request, you have to make sure the responseType parameter is set to blob. Once you get the response from the API, you will need to pass the Blob object (i.e., response.data) to the URL.createObjectURL() function. Below is a fully working example on how to create and download a file (Document), using … Read more

How to send a FastAPI response without redirecting the user to another page?

You would need to use a Javascript interface/library, such as Fetch API, to make an asynchronous HTTP request. Also, you should use Templates to render and return a TemplateResponse, instead of FileResponse, as shown in your code. Related answers can also be found here and here, which show how to handle <form> submission on the … Read more

Redirect to login page if user not logged in using FastAPI-Login package

From the code snippet you provided, you seem to be using the (third-party) FastAPI-Login package. Their documentation suggests using a custom Exception on the LoginManager instance, which can be used to redirect the user to the login page, if they are not logged in. Working example: The authentication below is based on cookies, but you … Read more

How to post JSON data to FastAPI and retrieve the JSON data inside the endpoint?

You should use the json parameter instead (which would change the Content-Type header to application/json): payload = {‘labels’: labels, ‘sequences’: sequences} r = requests.post(url, json=payload) not data which is used for sending form data with the Content-Type being application/x-www-form-urlencoded by default, or multipart/form-data if files are also included in the request—unless you serialised your JSON … Read more

Python ModuleNotFoundError while importing a module in conftest for Pytest framework

Option 1 Use relative imports: from ..src.data_kart.main import fastapi_app fastapi_app() Please note, the above requires running conftest.py outside the project’s directory, like this: python -m mt-kart.tests.conftest Option 2 Use the below in conftest.py (ref): import sys import os # getting the name of the directory where the this file is present. current = os.path.dirname(os.path.realpath(__file__)) # … 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