Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inability to redirect from a POST to a GET #229

Closed
syntaqx opened this issue Oct 7, 2015 · 4 comments
Closed

Inability to redirect from a POST to a GET #229

syntaqx opened this issue Oct 7, 2015 · 4 comments

Comments

@syntaqx
Copy link
Contributor

syntaqx commented Oct 7, 2015

func getHandler(c *echo.Context) error {
    return c.String("Hello world")
}

func postHandler(c *echo.Context) error {
    return echo.Redirect(http.StatusTemporaryRedirect, "/")
}

func main() {
    e := echo.New()

    e.Get("/", getHandler)
    e.Post("/post", postHandler)

    e.Run(":8080")
}

I whipped up that example pretty quickly, so I'm not entirely sure it works PERFECT, but the case that it makes hold true. For situations where you would want to redirect from a POST to a GET (for example, POST to a login form, and then redirect them back to the index), echo.Redirect fails. The redirect actually redirects to POST / instead of the expected GET /

I found some resources regarding this in Go that were solved in 2012, but has been resolved for some time. I'm assuming this might be within echo instead of Go's HTTP layer directly.

@syntaqx
Copy link
Contributor Author

syntaqx commented Oct 9, 2015

Bump - Anyone have any thoughts on this?

@vishr
Copy link
Member

vishr commented Oct 9, 2015

I just played around with redirect codes and found the following information:

307 was introduced to allow servers to make it clear to the user agent that a method change should not be made by the client when following the Location response header.

http://stackoverflow.com/a/2068504/197473

So you in this case you should use 301 or 302?

@syntaqx
Copy link
Contributor Author

syntaqx commented Oct 10, 2015

Makes sense actually. Looks like this works if using http.StatusFound - Kinda unexpected behavior, but not the fault of Go.

Thanks!

@syntaqx syntaqx closed this as completed Oct 10, 2015
@gocs
Copy link

gocs commented Apr 22, 2023

Makes sense actually. Looks like this works if using http.StatusFound - Kinda unexpected behavior, but not the fault of Go.

Thanks!

Any HTTP status codes can't work (or else, it proceeds to post again)

http.Redirect(w, r, r.Referer(), http.StatusOK)

HTTP Code has to be StatusFound or StatusMovedPermanently

http.Redirect(w, r, r.Referer(), http.StatusFound)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants