Gin 获取请求参数

1. 获取请求路由参数

package main

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

func main() {
    r := gin.Default()

    // 获取请求路由参数
    // curl -X GET "http://localhost:8080/video/15"
    r.GET("/:name/:id", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "name": c.Param("name"),
            "id" : c.Param("id"),
        })
    })

    r.Run(":8080")
}

输出

{
    "id": "15",
    "name": "video"
}

2. 获取 GET 请求参数

package main

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

func main() {
    r := gin.Default()

    // 获取 GET 请求参数
    // curl -X GET "http://127.0.0.1:8080/test?first_name=张&last_name=三"
    r.GET("/test", func(c *gin.Context) {
        firstName := c.Query("first_name")
        lastName := c.DefaultQuery("last_name", "Ben")
        c.JSON(http.StatusOK, gin.H{
            "first_name": firstName,
            "last_name": lastName,
        })
    })

    r.Run(":8080")
}

输出

{
    "first_name": "张",
    "last_name": "三"
}

3. 获取 POST 请求参数

package main

import (
    "bytes"
    "github.com/gin-gonic/gin"
    "io/ioutil"
)

func main() {
    r := gin.Default()

    // 获取 POST 请求参数
    // curl -X POST "http://127.0.0.1:8080/test_post" -d 'name="张三丰"'
    r.POST("/test_post", func(c *gin.Context) {
        body, err := ioutil.ReadAll(c.Request.Body)
        if err != nil {
            c.String(400, err.Error())
            c.Abort()
        }

        c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
        name := c.PostForm("name")
        age := c.DefaultPostForm("age", "18")

        c.JSON(200, gin.H{
            "body": string(body),
            "name": name,
            "age": age,
        })
    })

    r.Run(":8080")
}

4. 获取 Body 值

package main

import (
    "github.com/gin-gonic/gin"
    "io/ioutil"
)

func main() {
    r := gin.Default()

    // 获取 Body 值
    // curl -X POST "http://127.0.0.1:8080/test" -d '{"name": "张三丰"}'
    r.POST("/test", func(c *gin.Context) {
        body, err := ioutil.ReadAll(c.Request.Body)
        if err != nil {
            c.String(400, err.Error())
            c.Abort()
        }

        c.JSON(200, gin.H{
            "body": string(body),
        })
    })

    r.Run(":8080")
}

5. 获取参数绑定结构体

获取参数 bind 绑定结构

package main

import (
    "github.com/gin-gonic/gin"
    "time"
)

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

func main() {
    r := gin.Default()

    // curl -X GET 'http://localhost:8080/testing?name=张三&address=广东&birthday=2019-01-02'
    r.GET("/testing", testing)

    // curl -X POST 'http://localhost:8080/testing' -d 'name=张三&address=广东&birthday=2019-01-02'
    // curl -H "Content-Type:application/json" -X POST "http://localhost:8080/testing" -d '{"name": "李四"}'
    r.POST("/testing", testing)

    r.Run()
}

func testing(c *gin.Context)  {
    var person Person
    // 这里是根据请求 content-type 来做不同的 binding 操作
    if err := c.ShouldBind(&person);err != nil {
        c.String(200, "person bind error:%v", err)
    }
    c.String(200, "%v", person)
}
讨论数量: 0

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!