獲取Post中參數


package main

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

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

	router.POST("/form_post", func(c *gin.Context) {
		message := c.PostForm("username")
		nick := c.DefaultPostForm("userpassword", "????") // 此方法可以設置默認值

		// gin.H 實際上就是 map[string]interface{}
		c.JSON(200, gin.H{
			"status":  "posted",
			"username": message,
			"userpassword":    nick,
		})
	})
	router.Run(":8080")
} 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form action="http://localhost:8080/form_post" method="post" action="application/x-www-form-urlencoded">
    用戶名:<input type="text" name="username" placeholder="請輸入你的用戶名">  <br>
    密   碼:<input type="password" name="userpassword" placeholder="請輸入你的密碼">  <br>
    <input type="submit" value="提交">
</form>
</body>
</html>

 


免責聲明!

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



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