Fastapi: [QUESTION] How to post/redirect/get

Created on 15 Jan 2020  路  3Comments  路  Source: tiangolo/fastapi

First check

  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the FastAPI documentation, with the integrated search.
  • [X] I already searched in Google "How to post/redirect/get in FastAPI" and didn't find any information.

Description

How can I post/redirect/get?

@app.post("/")
async def login():
     return RedirectResponse(url="/ressource/1")

@app.get("/ressource/{r_id}")
async def get_ressource(r_id: UUID):
     return {"r_id": r_id}
question

Most helpful comment

I found the way to do this. You need to change the redirect default code 307 to 302.

...
return RedirectResponse(url="/ressource/1", status_code=HTTP_302_FOUND)
...

All 3 comments

I found the way to do this. You need to change the redirect default code 307 to 302.

...
return RedirectResponse(url="/ressource/1", status_code=HTTP_302_FOUND)
...

Thanks for reporting back and closing the issue :+1:

You can also use 303 (HTTP_303_SEE_OTHER) which was designed to handle redirects after a POST request. It's also mentioned on wiki for PRG pattern https://en.wikipedia.org/wiki/Post/Redirect/Get

Was this page helpful?
0 / 5 - 0 ratings