1. Json 数据解析和绑定
- 客户端传参,后端接收并解析到结构体
package mainimport ("github.com/gin-gonic/gin""net/http"
)// 定义接收数据的结构体
type Login struct {// binding:"required"修饰的字段,若接收为空值,则报错,是必须字段User string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"`Pssword string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
}func main() {// 1.创建路由// 默认使用了2个中间件Logger(), Recovery()r := gin.Default()// JSON绑定r.POST("loginJSON", func(c *gin.Context) {// 声明接收的变量var json Login// 将request的body中的数据,自动按照json格式解析到结构体if err := c.ShouldBindJSON(&json); err != nil {// 返回错误信息// gin.H封装了生成json数据的工具c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return}// 判断用户名密码是否正确if json.User != "root" || json.Pssword != "admin" {c.JSON(http.StatusBadRequest, gin.H{"status": "304"})return}c.JSON(http.StatusOK, gin.H{"status": "200"})})r.Run(":8000")
}
2. 表单数据解析和绑定
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title>
</head>
<body><form action="http://localhost:8000/loginForm" method="post" enctype="application/x-www-form-urlencoded">用户名<input type="text" name="username"><br>密码<input type="password" name="password"><input type="submit" value="提交"></form>
</body>
</html>
package mainimport ("net/http""github.com/gin-gonic/gin"
)// 定义接收数据的结构体
type Login struct {// binding:"required"修饰的字段,若接收为空值,则报错,是必须字段User string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"`Pssword string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
}func main() {// 1.创建路由// 默认使用了2个中间件Logger(), Recovery()r := gin.Default()// JSON绑定r.POST("/loginForm", func(c *gin.Context) {// 声明接收的变量var form Login// Bind()默认解析并绑定form格式// 根据请求头中content-type自动推断if err := c.Bind(&form); err != nil {c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return}// 判断用户名密码是否正确if form.User != "root" || form.Pssword != "admin" {c.JSON(http.StatusBadRequest, gin.H{"status": "304"})return}c.JSON(http.StatusOK, gin.H{"status": "200"})})r.Run(":8000")
}
3. URI数据解析和绑定
package mainimport ("net/http""github.com/gin-gonic/gin"
)// 定义接收数据的结构体
type Login struct {// binding:"required"修饰的字段,若接收为空值,则报错,是必须字段User string `form:"username" json:"user" uri:"user" xml:"user" binding:"required"`Pssword string `form:"password" json:"password" uri:"password" xml:"password" binding:"required"`
}func main() {// 1.创建路由// 默认使用了2个中间件Logger(), Recovery()r := gin.Default()// JSON绑定r.GET("/:user/:password", func(c *gin.Context) {// 声明接收的变量var login Login// Bind()默认解析并绑定form格式// 根据请求头中content-type自动推断if err := c.ShouldBindUri(&login); err != nil {c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})return}// 判断用户名密码是否正确if login.User != "root" || login.Pssword != "admin" {c.JSON(http.StatusBadRequest, gin.H{"status": "304"})return}c.JSON(http.StatusOK, gin.H{"status": "200"})})r.Run(":8000")
}
参考文章:
https://www.fansimao.com/928784.html
https://www.fansimao.com/928797.html
https://www.fansimao.com/928805.html