Gin框架-绑定Get参数或者Post参数

查看详细信息,这个例子很有用,可以自己实践一下

package main

import (
    "log"
    "time"

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

type Person struct {
    Name     string    `form:"name"`
    Address  string    `form:"address"`
    Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`
}

func main() {
    route := gin.Default()
    route.GET("/testing", startPage)
    route.Run(":8085")
}

func startPage(c *gin.Context) {
    var person Person
    // If `GET`, only `Form` binding engine (`query`) used.
    // 如果是Get,那么接收不到请求中的Post的数据??
    // 如果是Post, 首先判断 `content-type` 的类型 `JSON` or `XML`, 然后使用对应的绑定器获取数据.
    // See more at https://github.com/gin-gonic/gin/blob/master/binding/binding.go#L48
    if c.ShouldBind(&person) == nil {
        log.Println(person.Name)
        log.Println(person.Address)
        log.Println(person.Birthday)
    }

    c.String(200, "Success")
}

作者:正在修炼的西瓜君
链接:https://www.jianshu.com/p/98965b3ff638/
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Leave a Reply

Your email address will not be published. Required fields are marked *