go gin frame transformation get the request body

Copyright: by DongBao https://blog.csdn.net/aaaadong/article/details/90750918
package main

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

func main() {
	router := gin.Default()
	//http://localhost:8080/sayHellow?name=tom&id=2
	router.GET("/sayHellow", SayHello)

	// 默认启动的是 8080端口,也可以自己定义启动端口
	router.Run()
}

func SayHello(c *gin.Context) {
	//get 参数被重写了
	//c.Request.URL.RawQuery = "name=fix&id=3"
	var queryParams struct {
		Name string `form:"name" json:"name"` // 姓名
		Id   int    `form:"id" json:"id"`     // id
	}
	if c.BindQuery(&queryParams) != nil {
		c.String(400, "参数错误")
		return
	}
	fmt.Println(queryParams.Id, queryParams.Name)
}

1. Run the main method

 2. Access

http://localhost:8080/sayHellow?name=tom&id=2

3. Then

c.Request.URL.RawQuery = "name=fix&id=3"

Uncomment

4. Access

http://localhost:8080/sayHellow?name=tom&id=2

Different results prove intercept get the request body to take effect

Guess you like

Origin blog.csdn.net/aaaadong/article/details/90750918