我正在编写一个接受请求的快速 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
原文由 Werner Raath 发布,翻译遵循 CC BY-SA 4.0 许可协议
在文档中,他们讨论 了直接使用 Request ,这让我想到了 这一点: