5
votes

I'm a newbie when it comes to Go and Gin, so please excuse my ignorance.

I've setup a server using Gin which supports a POST request. I would like the user to POST their request which includes a required JSON payload redirecting that request to another URL. As part of the redirect I need to pass the original JSON payload. For example, if the user issues this CURL request:

curl -H "Content-Type: application/json" -d '{ "name": "NewTest Network", "organizationId": 534238, "type": "wireless"}' -X POST "http://localhost:8080/network"

My Gin code does this:

r.POST("/network", func(c *gin.Context) {
    c.Redirect(http.StatusMovedPermanently, networks_url)
})

where: networks_url is the redirected URL. I need a way to pass the original JSON payload to the redirected URL.

Any help you could provide would be greatly appreciated.

3

3 Answers

4
votes

This doesn't have anything to do with Go or gin.

This is the expected behavior of a user agent, which should change the method from POST to GET with either a 301 or 302 redirect.

To instruct the user agent to repeat the request with the same method, use 307 (http.StatusTemporaryRedirect) or 308 (http.StatusPermanentRedirect).

3
votes

You must use 301 or 302 as a redirect status code.

c.Redirect(301, "http://www.google.com/")

Please refer the documentation here.

0
votes

I've got the problem too many requests (loop), so I'm fine with this one.

if err != nil {
    session.AddFlash("The error I would like to show.", "error")
    c.Redirect(http.StatusMovedPermanently, "/my/route")
    return
}