question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to redirect to another page

See original GitHub issue

How can I redirect to another page/endpoint after, for instance, authenticating a user?

In flask, we can use ‘request’ function to redirect to a another page/endpoint, but I don’t see how to do it with FastAPI.

from flask import Flask,redirect

app = Flask(__name__)

@app.route('/')
def hello():
    return redirect("http://www.example.com")

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (13 by maintainers)

github_iconTop GitHub Comments

37reactions
tiangolocommented, May 15, 2019

Thanks @euri10 !

Before you commit the time to adding it in the docs, @marcosmmb, does @euri10’s example solve your use case?

Let me copy it inline here:

import logging

from fastapi import FastAPI
from starlette.responses import RedirectResponse
from starlette.testclient import TestClient

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

app = FastAPI()


@app.get("/app")
def read_main():
    return {"message": "Hello World from main app"}


subapi = FastAPI(openapi_prefix="/subapi")


@subapi.get("/sub")
async def read_sub():
    return {"message": "Hello World from sub API"}


@subapi.get("/redirect")
async def redirect():
    url = app.url_path_for("redirected")
    response = RedirectResponse(url=url)
    return response


@subapi.get("/redirected")
async def redirected():
    logger.debug("REDIRECTED")
    return {"message": "you've been redirected"}


app.mount("/subapi", subapi)


client = TestClient(app)


def test_redirect_subapi():
    url = app.url_path_for("redirect")
    response = client.get(url)
    assert response.json() == {"message": "you've been redirected"}
15reactions
euri10commented, May 7, 2019

simple code tested and working, couldn’t while afk, the logs show clearly you’re redirected to /redirected once you hit /redirect

hope it helps

@app.get("/redirect")
async def redirect():
    response = RedirectResponse(url='/redirected')
    return response


@app.get("/redirected")
async def redirected():
    logger.debug("debug message")
    return {"message": "you've been redirected"}
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,342 - uvicorn - DEBUG - ('192.168.96.1', 39694) - Connected
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,342 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [6] Initialized {'type': 'http', 'http_version': '1.1', 'server': ('192.168.96.3', 8000), 'client': ('192.168.96.1', 39694), 'scheme': 'http', 'method': 'GET', 'root_path': '', 'path': '/redirect', 'query_string': b'', 'headers': '<...>'}
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,343 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [6] Started task
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,344 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [6] Received {'type': 'http.response.start', 'status': 302, 'headers': '<...>'}
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,344 - uvicorn - INFO - ('192.168.96.1', 39694) - "GET /redirect HTTP/1.1" 302
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,344 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [6] Received {'type': 'http.response.body', 'body': '<0 bytes>'}
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,344 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [6] Completed
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,349 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [7] Initialized {'type': 'http', 'http_version': '1.1', 'server': ('192.168.96.3', 8000), 'client': ('192.168.96.1', 39694), 'scheme': 'http', 'method': 'GET', 'root_path': '', 'path': '/redirected', 'query_string': b'', 'headers': '<...>'}
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,349 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [7] Started task
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,350 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [7] Received {'type': 'http.response.start', 'status': 200, 'headers': '<...>'}
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,350 - uvicorn - INFO - ('192.168.96.1', 39694) - "GET /redirected HTTP/1.1" 200
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,350 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [7] Received {'type': 'http.response.body', 'body': '<36 bytes>'}
backend_1_d9db6d632ea8 | 2019-05-07 06:58:12,351 - uvicorn - DEBUG - ('192.168.96.1', 39694) - ASGI [7] Completed
backend_1_d9db6d632ea8 | 2019-05-07 06:58:17,351 - uvicorn - DEBUG - ('192.168.96.1', 39694) - Disconnected
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I redirect to another webpage? - javascript
One does not simply redirect using jQuery. jQuery is not necessary, and window.location.replace(...) will best simulate an HTTP redirect.
Read more >
5 ways to redirect a web page using JavaScript and jQuery
Another way to redirect to a web page in jQuery is by using the replace() function of the window.location object. This method removes...
Read more >
Redirect page to another form based on users answer - Jotform
Is it possible to direct a user to a particular form based on the initial question. For example, if the user selects yes...
Read more >
Redirect quiz to another page - RevenueHunt
If you'd like to redirect customers to another page after the quiz, you can try one of the methods listed below.
Read more >
Redirects and Google Search | Documentation
If you just want to send users to a different page temporarily, use a temporary redirect. This will also ensure that Google keeps...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found