提问者:小点点

如何在FastAPI中列出所有已定义的URL路径?


假设我有一个包含100+APIendpoint的FastAPI项目。 如何列出所有API/路径?


共1个答案

匿名用户

你可以试试这个,

from application import app

url_list = [
    {'path': route.path, 'name': route.name}
    for route in app.routes
]

这里的appFastAPI类的实例。

from fastapi import FastAPI

app = FastAPI()


@app.get(path='/', name='API Foo')
def foo():
    return {'message': 'this is API Foo'}


@app.post(path='/bar', name='API Bar')
def bar():
    return {'message': 'this is API Bar'}


@app.get('/url-list')
def get_all_urls():
    url_list = [
        {'path': route.path, 'name': route.name}
        for route in app.routes
    ]
    return url_list