• Gin框架系列之请求参数传递


    一、路径参数

    func main() {
        router := gin.Default()
    
        // This handler will match /user/john but will not match /user/ or /user
        router.GET("/user/:name", func(c *gin.Context) {
            name := c.Param("name")
            c.String(http.StatusOK, "Hello %s", name)
        })
    
        // However, this one will match /user/john/ and also /user/john/send
        // If no other routers match /user/john, it will redirect to /user/john/
        router.GET("/user/:name/*action", func(c *gin.Context) {
            name := c.Param("name")
            action := c.Param("action")
            message := name + " is " + action
            c.String(http.StatusOK, message)
        })
    
        // For each matched request Context will hold the route definition
        router.POST("/user/:name/*action", func(c *gin.Context) {
            b := c.FullPath() == "/user/:name/*action" // true
            c.String(http.StatusOK, "%t", b)
        })
    
        // This handler will add a new router for /user/groups.
        // Exact routes are resolved before param routes, regardless of the order they were defined.
        // Routes starting with /user/groups are never interpreted as /user/:name/... routes
        router.GET("/user/groups", func(c *gin.Context) {
            c.String(http.StatusOK, "The available groups are [...]")
        })
    
        router.Run(":8080")
    }

    二、请求参数

    (一)get请求参数

    package main
    
    import (
        "net/http"
    
        "github.com/gin-gonic/gin"
    )
    
    func main() {
        router := gin.Default()
        // 匹配url格式:/welcome?firstname=Jine&lastname=Doe
        router.GET("/welcome", func(c *gin.Context) {
            firstname := c.DefaultQuery("firstname", "Guest")
            lastname := c.Query("lastname")
            c.String(http.StatusOK, "Hello %s %s", firstname, lastname)
        })
        router.Run(":8000")
    }

    (二)post请求参数

    package main
    
    import (
        "net/http"
    
        "github.com/gin-gonic/gin"
    )
    
    /*
    可以通过postman或者python发送post请求
    import requests
     rsp = requests.post("http://127.0.0.1:8000/form_post", data={
         "message":"hello",
         "name":"lily"
     })
    */
    func main() {
        router := gin.Default()
        router.POST("/form_post", func(c *gin.Context) {
            message := c.PostForm("message")
            name := c.DefaultPostForm("name", "ok") //可以设置默认值
            c.JSON(http.StatusOK, gin.H{
                "message": message,
                "name":    name,
            })
        })
        router.Run(":8000")
    }

    (三)混合参数 

    package main
    
    import (
        "fmt"
    
        "github.com/gin-gonic/gin"
    )
    
    /*
    发送请求:
     rsp = requests.post("http://127.0.0.1:8000/post?id=1&page=2", data={
         "name":"hali",
         "message":"hello"
     })
    */
    func main() {
        router := gin.Default()
    
        router.POST("/post", func(c *gin.Context) {
            id := c.Query("id")
            page := c.DefaultQuery("page", "0")
            name := c.PostForm("name")
            message := c.PostForm("message")
            fmt.Printf("id: %s; page: %s; name: %s; message: %s", id, page, name, message)
    
        })
    
        router.Run(":8000")
    }
  • 相关阅读:
    SDU暑期集训排位(8)
    hdu1423 最长公共上升子序列
    poj2385 Apple Catching (线性dp)
    hdu5857 Median(模拟)
    hdu5858 Hard problem(求两圆相交面积)
    shuoj 1 + 2 = 3? (二分+数位dp)
    Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)
    hdu4734 F(x)(数位dp)
    hdu3709 Balanced Number (数位dp)
    hdu3652 B-number(数位dp)
  • 原文地址:https://www.cnblogs.com/shenjianping/p/15876586.html
Copyright © 2020-2023  润新知