1. 前言

对一些请求参数,gin框架提供了绑定和验证,我们常用于数据请求登录绑定等业务。
my-gin项目将此业务实现逻辑放在routers/data.go

2. Json 数据解析和绑定

package routers

import (
    "net/http"

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

// 定义接收数据的结构体,此处用作接收参数的验证
type Login struct {
    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 DataRouter(e *gin.Engine) {

    // 使用路由组
    r := e.Group("/data")

    // Json模型绑定和验证
    r.POST("/loginJson", loginJson)
}

//Json 模型绑定和验证
//curl 127.0.0.1:8080/data/loginJson -H 'content-type:application/json' -d "{\"user\":\"root\",\"password\":\"admin\"}" -X POST
func loginJson(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"})
}

电脑 win+R,cmd 执行 curl 127.0.0.1:8080/data/loginJson -H 'content-type:application/json' -d "{\"user\":\"root\",\"password\":\"admin\"}" -X POST
或者下载postman执行

3. 表单模型绑定和验证

package routers

import (
    "net/http"

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

// 定义接收数据的结构体,此处用作接收参数的验证
type Login struct {
    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 DataRouter(e *gin.Engine) {

    // 使用路由组
    r := e.Group("/data")

    // 表单模型绑定和验证
    r.POST("/loginForm", loginForm)
}

// 表单模型绑定和验证
func loginForm(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"})
}

添加表单页面显示,my-gin/data/loginForm.html

<!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>听说你在学习git</title>
</head>
<body>
<form action="http://localhost:8080/data/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>

打开页面my-gin/data/loginForm.html就可看到效果

4. URI模型绑定和验证

package routers

import (
    "net/http"

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

// 定义接收数据的结构体,此处用作接收参数的验证
type Login struct {
    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 DataRouter(e *gin.Engine) {

    // 使用路由组
    r := e.Group("/data")

    // URI模型绑定和验证
    r.GET("/:user/:password", user)
}

// URI模型绑定和验证
func user(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"})
}

http://127.0.0.1:8080/data/root/admin

Copyright © yzx该文章修订时间: 2021-09-08 16:55:18

results matching ""

    No results matching ""