Hi there!
I want to change body of POST request and redirect to other service with POST method. But "Redirect" method is doing GET request to other service. How to make POST redirect?
app.Post("/old", func(c *fiber.Ctx) error {
c.Response().SetBody([]byte("new body"))
return c.Redirect("http://localhost:8787/new")
})
Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
Hi @zhs!
c.Redirect, by default, sends a status code of 302 Found. By definition, this tells the browser to send a GET request to whatever URL is specified.
To avoid this, you can use 307 Temporary Redirect.
The only difference between 307 and 302 is that 307 guarantees that the method and the body will not be changed when the redirected request is made. With 302, some old clients were incorrectly changing the method to GET: the behavior with non-GET methods and 302 is then unpredictable on the Web, whereas the behavior with 307 is predictable.
To tell c.Redirect to use 307, you can use the following:
app.Post("/old", func (c *fiber.Ctx) error {
return c.Redirect("http://localhost:8787/new", fiber.StatusTemporaryRedirect)
})
Most helpful comment
Hi @zhs!
c.Redirect, by default, sends a status code of302 Found. By definition, this tells the browser to send a GET request to whatever URL is specified.To avoid this, you can use
307 Temporary Redirect.To tell
c.Redirectto use307, you can use the following: