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")
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM