starlette

How to raise custom exceptions in a FastAPI middleware?

How to raise custom exceptions in a FastAPI middleware? Question: I have a simple FastAPI setup with a custom middleware class inherited from BaseHTTPMiddleware. Inside this middleware class, I need to terminate the execution flow under certain conditions. So, I created a custom exception class named CustomError and raised the exception. from fastapi import FastAPI, …

Total answers: 2

How to re-route requests to a different URL/endpoint in FastAPI?

How to re-route requests to a different URL/endpoint in FastAPI? Question: I am trying to write a middleware in my FastAPI application, so that requests coming to endpoints matching a particular format will be re-routed to a different URL, but I am unable to find a way to do that since request.url is read-only. I …

Total answers: 2

Python and Starlette: running a long async task

Python and Starlette: running a long async task Question: I have a simple experiment in the code snippet shown below. My goal is to have the browser client (via a WebSocket) kick off a long-running task on the server, but the server should service WebSocket messages from the client while the long-running task is running. …

Total answers: 1

FastAPI/Starlette: How to handle exceptions inside background tasks?

FastAPI/Starlette: How to handle exceptions inside background tasks? Question: I developed some API endpoints using FastAPI. These endpoints are allowed to run BackgroundTasks. Unfortunately, I do not know how to handle unpredictable issues from theses tasks. An example of my API is shown below: # main.py from fastapi import FastAPI import uvicorn app = FastAPI() …

Total answers: 2

How to cache StreamingResponse?

How to cache StreamingResponse? Question: Is that possible to cache StreamingResponse? I saw, a couple of tickets related to this topic (e.g. here), but none of them gave clear answer. I tried aiocache package and fastapi-cache package, but all this kind of packages try to convert value being cached to JSON or even a string, …

Total answers: 1

Why url_for generates URL with localhost as the hostname instead of the domain name?

Why url_for generates URL with localhost as the hostname instead of the domain name? Question: I have a FastAPI web application using Jinja2 templates, which is working fine on localhost, but not in production. The problem is that is not generating URLs for JavaScript and other static files correctly. I have deployed it on EC2 …

Total answers: 2

How to get route's name using FastAPI/Starlette?

How to get route's name using FastAPI/Starlette? Question: How can I get the name of a route/endpoint using FastAPI/Starlette? I have access to the Request object and I need this information in one of my middlewares. For example, if I hit services/1, I should then be able to get the abc name. Is this possible …

Total answers: 1

How to read the request body using orjson library in FastAPI?

How to read the request body using orjson library in FastAPI? Question: I am writing code to receive a JSON payload in FastAPI. Here is my code: from fastapi import FastAPI, status, Request from fastapi.responses import ORJSONResponse import uvicorn import asyncio import orjson app = FastAPI() @app.post("/", status_code = status.HTTP_200_OK) async def get_data(request: Request): param …

Total answers: 1

How to properly use Regex in CORS Middleware for FastAPI?

How to properly use Regex in CORS Middleware for FastAPI? Question: I have an app that uses a FastAPI backend and a Next.js frontend. In development and on production with stable origins, I am able to use the CORSMiddleware with no issues. However, I have deployed the Next.js frontend with Vercel, and want to take …

Total answers: 3

FastAPI does not replace "+" plus symbol in GET request

FastAPI does not replace "+" plus symbol in GET request Question: I understand this is not a FastAPI issue, but how to avoid this using FastAPI? For example: from fastapi import FastAPI app = FastAPI() @app.get(‘/’) async def root(q: str): return {"message": f"{q}"} Issuing the following request: http://127.0.0.1:8000/?q=1+1 returns: {"message":"1 1"} Asked By: Vyacheslav || …

Total answers: 2