For redirecting from a POST
to a GET
method, please have a look at this and this answer on how to do that and the reason for using status_code=status.HTTP_303_SEE_OTHER
(example is given below).
As for the reason for getting starlette.routing.NoMatchFound
error, this is because request.url_for()
receives path
parameters, not query
parameters. Your msg
and result
parameters are query
ones; hence, the error.
A solution would be to use a CustomURLProcessor
, as suggested in this and this answer, allowing you to pass both path
(if need to) and query
parameters to the url_for()
function and obtain the URL. As for hiding the path
and/or query
parameters from the URL, you can use a similar approach to this answer that uses history.pushState()
(or history.replaceState()
) to replace the URL in the browser’s address bar.
Complete working example can be found below (you can use your own TemplateResponse
in the place of HTMLResponse
).
from fastapi import FastAPI, Request, status
from fastapi.responses import RedirectResponse, HTMLResponse
from typing import Optional
import urllib
app = FastAPI()
class CustomURLProcessor:
def __init__(self):
self.path = ""
self.request = None
def url_for(self, request: Request, name: str, **params: str):
self.path = request.url_for(name, **params)
self.request = request
return self
def include_query_params(self, **params: str):
parsed = list(urllib.parse.urlparse(self.path))
parsed[4] = urllib.parse.urlencode(params)
return urllib.parse.urlunparse(parsed)
@app.get("https://stackoverflow.com/", response_class=HTMLResponse)
def event_msg(request: Request, msg: Optional[str] = None):
if msg:
html_content = """
<html>
<head>
<script>
window.history.pushState('', '', "/");
</script>
</head>
<body>
<h1>""" + msg + """</h1>
</body>
</html>
"""
return HTMLResponse(content=html_content, status_code=200)
else:
html_content = """
<html>
<body>
<h1>Create an event</h1>
<form method="POST" action="/">
<input type="submit" value="Create Event">
</form>
</body>
</html>
"""
return HTMLResponse(content=html_content, status_code=200)
@app.post("https://stackoverflow.com/")
def event_create(request: Request):
redirect_url = CustomURLProcessor().url_for(request, 'event_msg').include_query_params(msg="Succesfully created!")
return RedirectResponse(redirect_url, status_code=status.HTTP_303_SEE_OTHER)
Update
Regarding adding query params to url_for()
, another solution would be using Starlette’s starlette.datastructures.URL
, which now provides a method to include_query_params
. Example:
from starlette.datastructures import URL
redirect_url = URL(request.url_for('event_msg')).include_query_params(msg="Succesfully created!")
return RedirectResponse(redirect_url, status_code=status.HTTP_303_SEE_OTHER)