gpt4 book ai didi

python - 如何设置 "/*"路径以捕获 FastAPI 中的所有路由?

转载 作者:行者123 更新时间:2023-12-05 01:08:15 28 4
gpt4 key购买 nike

ExpressJS 一样,app.get("/*") 适用于所有路由。

我的代码 -

from typing import Optional
from fastapi import FastAPI

app = FastAPI()


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

试过了,网页结果显示{"detail":"Not Found"}
如何在 FastApi 中做到相同

最佳答案

您可以使用 /{full_path} 捕获一条路径中的所有路由(参见 documentation)。

@app.route("/{full_path:path}")
async def capture_routes(request: Request, full_path: str):
...

关于python - 如何设置 "/*"路径以捕获 FastAPI 中的所有路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66202833/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com