跳转到内容

查询字符串和表单

处理 POST 请求时,你通常需要同时从 URL 查询字符串和请求体中读取值。Gin 将这两个数据源分开,因此你可以独立访问每一个:

  • c.Query("key") / c.DefaultQuery("key", "default") —— 从 URL 查询字符串读取。
  • c.PostForm("key") / c.DefaultPostForm("key", "default") —— 从 application/x-www-form-urlencodedmultipart/form-data 请求体读取。

这在 REST API 中很常见,路由通过查询参数(如 id)标识资源,而请求体携带有效负载(如 namemessage)。

package main
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
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\n", id, page, name, message)
c.String(http.StatusOK, "id: %s; page: %s; name: %s; message: %s", id, page, name, message)
})
router.Run(":8080")
}

测试

Terminal window
# Query params in URL, form data in body
curl -X POST "http://localhost:8080/post?id=1234&page=1" \
-d "name=manu&message=this_is_great"
# Output: id: 1234; page: 1; name: manu; message: this_is_great
# Missing page -- falls back to default value "0"
curl -X POST "http://localhost:8080/post?id=1234" \
-d "name=manu&message=hello"
# Output: id: 1234; page: 0; name: manu; message: hello

另请参阅