Skip to content

请求数据参数绑定

为了能够更方便的获取请求相关参数,提高开发效率,我们可以基于请求的Content-Type识别请求数据类型并利用反射机制自动提取请求中QueryStringform表单JSONXML等参数到结构体中。

在 Go 中,通过反射解析数据都是存放在结构体中,所以我们先定义一个结构体用来接受数据,如下:

go
type Login struct{
    User string `form:"username" json:"username" xml:"username" binding:"required"`
    Password string `form:"username" json:"username" xml:"username" binding:"required"`
}

其中 :

  • form:会去解析 form 表单数据
  • json:会去解析 json 格式数据
  • xml:会去解析 xml 格式数据
  • binding:required 表示设置的参数是必须参数,如果没有传就会报错

解析 JSON 数据

go
package main

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

type Login struct{
	User string `form:"username" json:"username" xml:"username" binding:"required"`
	Password string `form:"password" json:"password" xml:"password" binding:"required"`
}

func main(){
	// 1、创建路由
	g := gin.Default()
	// 2、绑定路由规则
	g.POST("/loginJSON", func(context *gin.Context) {
		var login Login
		// 对参数进行绑定
		if err := context.ShouldBindJSON(&login);err != nil{
			// 如果有错误返回JSON数据
			context.JSON(304,gin.H{"status": err.Error()})
		}
		// 如果没有报错,取值并返回
		context.JSON(200,gin.H{
			"status": http.StatusOK,
			"username": login.User,
			"password": login.Password,
		})
	})
	g.Run(":8000")
}

测试看结果:

(1)、正常请求

6dac22c43844670d44d519f3e1084ea8 MD5

(2)、错误请求

b602bc7db05464d8a37f5371bcd5a952 MD5

解析 Form 表单

go
package main

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

type Login struct{
	User string `form:"username" json:"username" xml:"username" binding:"required"`
	Password string `form:"password" json:"password" xml:"password" binding:"required"`
}

func main(){
	// 1、创建路由
	g := gin.Default()
	// 2、绑定路由规则
	g.POST("/loginFORM", func(context *gin.Context) {
		var login Login
		// 对参数进行绑定
		if err := context.ShouldBind(&login);err != nil{
			// 如果有错误返回JSON数据
			context.JSON(304,gin.H{"status": err.Error()})
		}
		// 如果没有报错,取值并返回
		context.JSON(200,gin.H{
			"status": http.StatusOK,
			"username": login.User,
			"password": login.Password,
		})
	})
	g.Run(":8000")
}

135af2f9d6b660e49f63ae1cc1dd33fc MD5

解析 URL 数据

go
package main

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

type Login struct{
	User string `form:"username" json:"username" xml:"username" uri:"username" binding:"required"`
	Password string `form:"password" json:"password" xml:"password" uri:"password" binding:"required"`
}

func main(){
	// 1、创建路由
	g := gin.Default()
	// 2、绑定路由规则
	g.GET("/:username/:password", func(context *gin.Context) {
		var login Login
		// 对参数进行绑定
		if err := context.ShouldBindUri(&login);err != nil{
			// 如果有错误返回JSON数据
			context.JSON(304,gin.H{"status": err.Error()})
		}
		// 如果没有报错,取值并返回
		context.JSON(200,gin.H{
			"status": http.StatusOK,
			"username": login.User,
			"password": login.Password,
		})
	})
	g.Run(":8000")
}

15d8cd4459f8385c0efe88ed372e332b MD5

解析 queryString 数据

go
package main

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

type Login struct{
	User string `form:"username" json:"username" xml:"username" uri:"username" binding:"required"`
	Password string `form:"password" json:"password" xml:"password" uri:"password" binding:"required"`
}

func main(){
	// 1、创建路由
	g := gin.Default()
	// 2、绑定路由规则
	g.GET("/login", func(context *gin.Context) {
		var login Login
		// 对参数进行绑定
		if err := context.ShouldBind(&login);err != nil{
			// 如果有错误返回JSON数据
			context.JSON(304,gin.H{"status": err.Error()})
		}
		// 如果没有报错,取值并返回
		context.JSON(200,gin.H{
			"status": http.StatusOK,
			"username": login.User,
			"password": login.Password,
		})
	})
	g.Run(":8000")
}

aeece4a284b9e09e80a6bb677e9e46b2 MD5

最近更新