Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Gin: How to pass original JSON payload to a Redirect POST

Tags:

go

go-gin

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.

like image 467
user6938349 Avatar asked Oct 07 '16 16:10

user6938349


2 Answers

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

like image 175
JimB Avatar answered Sep 21 '22 07:09

JimB


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

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

Please refer the documentation here.

like image 35
Ravishankar S R Avatar answered Sep 20 '22 07:09

Ravishankar S R