Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set "/*" path to capture all routes in FastAPI?

Tags:

python

fastapi

Like in ExpressJS, app.get("/*") works for all routes.

My code -

from typing import Optional
from fastapi import FastAPI

app = FastAPI()


@app.get('/*')
def user_lost():
    return "Sorry You Are Lost !"

I tried it but the webpage result shows {"detail":"Not Found"}
How can I do the same in FastApi?

like image 663
Akash Pattnaik Avatar asked Oct 18 '25 09:10

Akash Pattnaik


1 Answers

You can use /{full_path} to capture all routes in one path (See documentation).

@app.route("/{full_path:path}")
async def capture_routes(request: Request, full_path: str):
    ...
like image 106
Yagiz Degirmenci Avatar answered Oct 20 '25 23:10

Yagiz Degirmenci