【发布时间】:2020-09-28 11:02:09
【问题描述】:
我正在编写一个快速 API 服务器,它接受请求,检查用户是否被授权,如果成功则将他们重定向到另一个 url。
我需要携带URL参数,例如
http://localhost:80/data/?param1=val1¶m2=val2 应该重定向到 http://some.other.api/?param1=val1¶m2=val2,从而保留之前分配的参数。
有些参数不是我控制的,随时可能改变。
我怎样才能做到这一点?
代码:
from fastapi import FastAPI
from starlette.responses import RedirectResponse
app = FastAPI()
@app.get("/data/")
async def api_data():
params = '' # I need this value
url = f'http://some.other.api/{params}'
response = RedirectResponse(url=url)
return response
【问题讨论】:
-
请尝试
@app.get("/files/{file_path:path}"), fastapi.tiangolo.com/tutorial/path-params