我有一个 python 应用程序在我的 localhost:3978 上运行。是否可以从 http://localhost:3978/api/accounts 向 http://localhost:3978/api/users 进行 api 调用?
@routes.get("/api/accounts")
async def accounts(request):
api_url = "http://127.0.0.1:3978/api/users"
response = requests.get(api_url)
return json_response(data=respone.json())
@routes.get("/api/users")
async def users(request):
pass
您可以使用
url_path_for()
来输入 RedirectResponse
中的 starlette.responses
,如此处所述。例如:
from fastapi import FastAPI
from starlette.responses import RedirectResponse
app = FastAPI()
@app.get("/a")
async def a():
return {"message": "Hello World"}
@app.get('/b')
async def b():
url = app.url_path_for("a")
response = RedirectResponse(url=url)
return response