gin中的重定向

package main

import (
	"github.com/gin-gonic/gin"
)

func main() {
	// HTTP重定向很容易,内部、外部重定向均支持
	router := gin.Default()

	/* GET重定向
	router.GET("/", func(context *gin.Context) {
		context.Redirect(http.StatusMovedPermanently, "http://www.mayanan.cn:5000")
	})
	 */

	/* POST重定向
	router.POST("/post", func(context *gin.Context) {
		context.Redirect(http.StatusFound, "/foo")
	})
	router.GET("/foo", func(context *gin.Context) {
		context.JSON(200, "post_redirect")
	})
	 */

	/* 路由重定向 */
	router.GET("/test", func(context *gin.Context) {
		context.Request.URL.Path = "/test02"
		router.HandleContext(context)
	})
	router.GET("/test02", func(context *gin.Context) {
		context.String(200, "test02")
	})

	router.Run()
}

  

posted @ 2021-10-29 10:01  专职  阅读(295)  评论(0编辑  收藏  举报