有默认值方式获取POST请求参数
package main
import "github.com/gin-gonic/gin"
func main() {
app := gin.Default()
app.Handle("POST", "/", func(context *gin.Context) {
name := context.DefaultPostForm("name", "李小花")
context.Writer.Write([]byte(name))
})
app.Run()
}
无默认值方式获取POST请求参数
package main
import "github.com/gin-gonic/gin"
func main() {
app := gin.Default()
app.Handle("POST", "/", func(context *gin.Context) {
name := context.PostForm("name")
context.Writer.Write([]byte(name))
})
app.Run()
}
获取POST请求参数的同时,返回参数获取状态,状态为bool类型
package main
import "github.com/gin-gonic/gin"
func main() {
app := gin.Default()
app.Handle("POST", "/", func(context *gin.Context) {
name, status := context.GetPostForm("name")
if !status {
panic("未获取到name参数")
}
context.Writer.Write([]byte(name))
})
app.Run()
}