Fiber: 馃 Redirect by POST request

Created on 27 Oct 2020  路  2Comments  路  Source: gofiber/fiber

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")
    })
馃 Question

Most helpful comment

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)
})

All 2 comments

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)
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

faultable picture faultable  路  3Comments

abowloflrf picture abowloflrf  路  4Comments

petersephrin picture petersephrin  路  4Comments

bashery picture bashery  路  4Comments

Terisback picture Terisback  路  3Comments