0

I have a python application running on my localhost:3978. Is it possible to make an api call to http://localhost:3978/api/users from http://localhost:3978/api/accounts?

@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
2
  • 1
    You can explicitly redirect to any other api using`` return redirect(url_for(func_name) ) `` else separate the controller logic from route and just call the controller function for that route. Commented Nov 23, 2021 at 11:20
  • In aiohttp, I know that aiohttp.web.HTTPFound('\someendpoint') redirects to the given endpoint and completes the execution, but I want it to come to the calling function. Commented Nov 24, 2021 at 13:08

1 Answer 1

1

You can use url_path_for() to feed in RedirectResponse of starlette.responses as stated here. For example:

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.