How to list all defined url paths in fastapi?

by hal.littel , in category: Javascript , 20 days ago

How to list all defined url paths in fastapi?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by dedrick , 19 days ago

@hal.littel 

To list all defined url paths in FastAPI, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from fastapi import FastAPI

app = FastAPI()

@app.get("/path1")
def path1():
    return {"message": "This is path1"}

@app.get("/path2")
def path2():
    return {"message": "This is path2"}

# Get all routes
routes = app.routes
for route in routes:
    print(route.path)

# Output:
# /path1
# /path2


This code creates a FastAPI app with two defined paths ("/path1" and "/path2"). It then retrieves all the routes defined in the app and prints out the path of each route. This way, you can see a list of all defined URL paths in FastAPI.