From 8446f6fb2a251be90bd65d15fb83872ed089340b Mon Sep 17 00:00:00 2001 From: Zeni Kim Date: Tue, 12 May 2026 23:21:03 -0500 Subject: [PATCH] In the core's response.go, add an optional RedirectCodeSetter field or variadic use303 ...bool parameter to Redirect() and use the stored code + http.StatusSeeOther in core.go instead of the hardcoded http.StatusTemporaryRedirect, so that a POST handler can redirect to a GET route by calling c.Response.Redirect("/url", true). --- core.go | 6 +++++- response.go | 14 ++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/core.go b/core.go index cf99f3e..4163e2d 100644 --- a/core.go +++ b/core.go @@ -308,7 +308,11 @@ func (app *App) makeHTTPRouterHandlerFunc(h Controller, ms []Hook) httprouter.Ha w.WriteHeader(ctx.Response.statusCode) } if ctx.Response.redirectTo != "" { - http.Redirect(w, r, ctx.Response.redirectTo, http.StatusTemporaryRedirect) + statusCode := ctx.Response.redirectStatusCode + if statusCode == 0 { + statusCode = http.StatusTemporaryRedirect // default to 307 + } + http.Redirect(w, r, ctx.Response.redirectTo, statusCode) } else { w.Write(ctx.Response.body) } diff --git a/response.go b/response.go index 7e92b2f..716eb05 100644 --- a/response.go +++ b/response.go @@ -20,6 +20,7 @@ type Response struct { overrideContentType string isTerminated bool redirectTo string + redirectStatusCode int HttpResponseWriter http.ResponseWriter } @@ -136,8 +137,10 @@ func (rs *Response) ForceSendResponse() { rs.isTerminated = true } -// updates the redirect URL for the response and returns the modified Response. Validates the URL before setting it. -func (rs *Response) Redirect(url string) *Response { +// Redirect sends a redirect response to the given URL. +// By default it uses 307 (Temporary Redirect) to preserve the HTTP method. +// Pass true as the second argument to use 303 (See Other), which changes POST to GET. +func (rs *Response) Redirect(url string, use303 ...bool) *Response { validator := resolveValidator() v := validator.Validate(map[string]interface{}{ "url": url, @@ -153,6 +156,13 @@ func (rs *Response) Redirect(url string) *Response { return rs } rs.redirectTo = url + + if len(use303) > 0 && use303[0] { + rs.redirectStatusCode = http.StatusSeeOther // 303 + } else { + rs.redirectStatusCode = http.StatusTemporaryRedirect // 307 (default) + } + return rs }