小编Kir*_*rks的帖子

FastAPI:如何从请求中获取原始 URL 路径?

我有一个GET在路径中带有请求参数的方法:

@router.get('/users/{user_id}')
async def get_user_from_string(user_id: str):
    return User(user_id)
Run Code Online (Sandbox Code Playgroud)

'/users/{user_id}'是否可以从请求中获取基本 url 原始路径(即)?

我尝试使用以下方式:

path = [route for route in request.scope['router'].routes if
        route.endpoint == request.scope['endpoint']][0].path
Run Code Online (Sandbox Code Playgroud)

但这不起作用,我得到:

属性错误:“安装”对象没有属性“端点”

python url-routing starlette fastapi

5
推荐指数
2
解决办法
9137
查看次数

标签 统计

fastapi ×1

python ×1

starlette ×1

url-routing ×1