go gin框架改造get请求体

版权声明: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.运行main方法

 2.访问

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

3.再把

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

取消注释

4.访问

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

结果不同,证明拦截get请求体生效

猜你喜欢

转载自blog.csdn.net/aaaadong/article/details/90750918