关于gin:gin-表单数据处理简单实例

3次阅读

共计 865 个字符,预计需要花费 3 分钟才能阅读完成。

package main

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

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

    r.POST("/json", func(c *gin.Context) {data,_ := c.GetRawData()
        var m map[string]interface{}
        _ = json.Unmarshal(data, &m)
        c.JSON(http.StatusOK,m)

    })

    r.LoadHTMLGlob("./temp/*")

    r.GET("/user/add", func(c *gin.Context) {c.HTML(http.StatusOK, "useradd.html", gin.H{})
    })

    r.POST("/user/add", func(c *gin.Context) {username := c.PostFormArray("username")
        password := c.PostFormArray("password")
        fmt.Println(username, "goubibibi", password)
        c.JSON(http.StatusOK,gin.H{
            "msg":"ok",
            "username":username,
            "password": password,
        })

    })


    r.Run()}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <form action="/user/add" method="post">
        <p>username: <input type="text" name="username"></p>
        <p>password: <input type="text" name="password"></p>
        <button type="submit"></button>
    </form>


</head>
<body>

</body>
</html>

正文完
 0