How to redirect the user to another page after login using JavaScript Fetch API?

The main “Problem” that I see that might cause this to not work is the fast that you’re doing it form a Post request to a Get request.

After some search online I’ve stumble across this [BUG] RedirectResponse from a POST request route to GET request route if you’ll read this bug you’ll see they specify that sometimes you may need a 307 instead you can read about the 307 response here 307 Temporary Redirect.

According to this the following should help:

import starlette.status as status
from fastapi.responses import RedirectResponse

@router.post("/login")
async def login(x_auth_token: str = Header(None))
    # Implementation details ...
    return RedirectResponse('/1', status_code=status.HTTP_302_FOUND)

@app.get("/1")
def get_landing(request: Request):
    return templates.TemplateResponse("landing.html", {"request": request})

From what I’ve seen the solution here was to use status_code=status.HTTP_302_FOUND you can learn more about it here: What Is a 302 Status Code?

You can also refer to the following links for more:

  1. fastapi (starlette) RedirectResponse redirect to post instead get method
  2. How to do a Post/Redirect/Get (PRG) in FastAPI?
  3. [QUESTION] How to post/redirect/get
  4. RedirectResponse

According to @Chris in the comments, you also have the following:

Leave a Comment