如何从go *gin.context对象中获取所有查询参数

huangapple go评论111阅读模式
英文:

How to get all query parameters from go *gin.context object

问题

我正在查看 https://godoc.org/github.com/gin-gonic/gin 的文档,寻找一个返回传递的所有查询参数列表的方法。有一些方法可以返回查询参数的值,但是否有返回所有查询参数列表的方法呢?如果无法获取值也没关系。我正在使用以下代码获取查询参数的值。但是这段代码只能检查查询参数是否存在。

  1. func myHandler(c *gin.Context) {
  2. // 检查查询参数
  3. if queryParam, ok := c.GetQuery("startingIndex"); ok {
  4. if queryParam == "" {
  5. c.Header("Content-Type", "application/json")
  6. c.JSON(http.StatusNotFound,
  7. gin.H{"Error: ": "Invalid startingIndex on search filter!"})
  8. c.Abort()
  9. return
  10. }
  11. }
  12. }
英文:

I am looking at https://godoc.org/github.com/gin-gonic/gin documentation for a method which returns list of all the query parameters passed. There are methods which return value of a query parameter. Is there any method which returns list of all the query parameters passed ? It's ok if we don't get values. I am fetching the values of the query parameter using the following code. But this code can only check if the query parameter exists or not.

  1. func myHandler(c *gin.Context) {
  2. // check for query params
  3. if queryParam, ok := c.GetQuery("startingIndex"); ok {
  4. if queryParam == "" {
  5. c.Header("Content-Type", "application/json")
  6. c.JSON(http.StatusNotFound,
  7. gin.H{"Error: ": "Invalid startingIndex on search filter!"})
  8. c.Abort()
  9. return
  10. }
  11. }
  12. }

答案1

得分: 71

你应该可以使用c.Request.URL.Query()来获取一个Values,它是一个map[string][]string类型的数据结构。

英文:

You should be able to do c.Request.URL.Query() which will return a Values which is a map[string][]string

答案2

得分: 26

你可以使用以下方式获取查询参数:

  1. c.Query("key") -> 返回字符串
  2. 或者
  3. c.GetQuery("key") -> 返回 (字符串, bool -> ok)
英文:

you can get query parameters using :

  1. c.Query("key") -> returns string
  2. or
  3. c.GetQuery("key") -> returns (string, bool->ok)

答案3

得分: 17

如果你在谈论 GET 查询参数,你可以使用以下方法获取它们:

  1. c.Request.URL.Query()

你将得到一个 Values 类型,它是一个 map[string][]string

文档:https://golang.org/pkg/net/url/#URL.Query

英文:

If you're talking about GET query params, you can retrieve them using:

  1. c.Request.URL.Query()

You'll get back a Values type which is a map[string][]string

Docs: https://golang.org/pkg/net/url/#URL.Query

答案4

得分: 0

继续@ctcherry的回答...

  1. func myAPIEndpoint(c *gin.Context) {
  2. paramPairs := c.Request.URL.Query()
  3. for key, values := range paramPairs {
  4. fmt.Printf("key = %v, value(s) = %v\n", key, values)
  5. }
  6. }

请记住,一个给定的键可能包含多个值,所以你的values是一个数组。

英文:

Carrying on from @ctcherry's answer...

  1. func myAPIEndpoint(c *gin.Context) {
  2. paramPairs := c.Request.URL.Query()
  3. for key, values := range paramPairs {
  4. fmt.Printf("key = %v, value(s) = %v\n", key, values)
  5. }
  6. }

Keep in mind that a given key may contain multiple values, so your values is an array.

huangapple
  • 本文由 发表于 2016年12月22日 17:02:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/41279297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定