From 41ad780073b3a0a815e0083ecf1a0cea38cb8105 Mon Sep 17 00:00:00 2001 From: Bartek Ciszkowski Date: Fri, 15 May 2020 03:42:12 -0400 Subject: [PATCH] RedirectResponse now accepts optional background parameter (#945) * RedirectResponse now accepts optional background parameter With this change, a `background` task can now be sent during a RedirectResponse. This can be useful when an application wishes to run a background task after a form submission, which usually includes a redirect (e.g. to prevent resubmission) Closes #941 * Revert testing background in redirect test --- starlette/responses.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/starlette/responses.py b/starlette/responses.py index 0a74b045..81168351 100644 --- a/starlette/responses.py +++ b/starlette/responses.py @@ -173,9 +173,15 @@ class UJSONResponse(JSONResponse): class RedirectResponse(Response): def __init__( - self, url: typing.Union[str, URL], status_code: int = 307, headers: dict = None + self, + url: typing.Union[str, URL], + status_code: int = 307, + headers: dict = None, + background: BackgroundTask = None, ) -> None: - super().__init__(content=b"", status_code=status_code, headers=headers) + super().__init__( + content=b"", status_code=status_code, headers=headers, background=background + ) self.headers["location"] = quote_plus(str(url), safe=":/%#?&=@[]!$&'()*+,;")