1. 表单参数

    • 表单传输为post请求,http常见的传输格式为四种:
      • application/json
      • application/x-www-form-urlencoded
      • application/xml
      • multipart/form-data
    • 表单参数可以通过PostForm()方法获取,该方法默认解析的是x-www-form-urlencoded或from-data格式的参数
    1. import (
    2. "fmt"
    3. "net/http"
    4. )
    5. func main() {
    6. r.POST("/form", func(c *gin.Context) {
    7. types := c.DefaultPostForm("type", "post")
    8. password := c.PostForm("userpassword")
    9. // c.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s,type:%s", username, password, types))
    10. c.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s,type:%s", username, password, types))
    11. })
    12. r.Run()