Redirects

Issuing a HTTP redirect is easy. Both internal and external locations are supported.

  1. r.GET("/test", func(c *gin.Context) {
  2. c.Redirect(http.StatusMovedPermanently, "http://www.google.com/")
  3. })

Issuing a HTTP redirect from POST. Refer to issue: #444

  1. r.POST("/test", func(c *gin.Context) {
  2. c.Redirect(http.StatusFound, "/foo")
  3. })

Issuing a Router redirect, use HandleContext like below.

  1. r.GET("/test", func(c *gin.Context) {
  2. c.Request.URL.Path = "/test2"
  3. r.HandleContext(c)
  4. })
  5. r.GET("/test2", func(c *gin.Context) {
  6. c.JSON(200, gin.H{"hello": "world"})
  7. })

Last modified May 27, 2020 : chore: Add a redirect sample for POST method (#124) (b7eaab3)