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

6

44

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.

func myHandler(c *gin.Context) {

    // check for query params
    if queryParam, ok := c.GetQuery("startingIndex"); ok {
        if queryParam == "" {
            c.Header("Content-Type", "application/json")
            c.JSON(http.StatusNotFound,
                gin.H{"Error: ": "Invalid startingIndex on search filter!"})
            c.Abort()
            return
        }
    }
}
Mailemailed answered 22/12, 2016 at 9:2 Comment(0)
C
77

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

Cherianne answered 22/12, 2016 at 9:22 Comment(2)
why choose map[string][]string instead of map[string]string?Britnibrito
In a query string, a param name can be used more than once, using []string for the value of the map allows them to capture all of different values that share the same param name. play.golang.org/p/FtQIz_ePF_NCherianne
W
30

you can get query parameters using :

c.Query("key") -> returns string
or
c.GetQuery("key") -> returns (string, bool->ok)
Wilful answered 14/6, 2021 at 8:31 Comment(0)
P
18

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

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

Psychodiagnostics answered 22/12, 2016 at 9:21 Comment(0)
U
0

Carrying on from @ctcherry's answer...

func myAPIEndpoint(c *gin.Context) {

    paramPairs := c.Request.URL.Query()
    for key, values := range paramPairs {
        fmt.Printf("key = %v, value(s) = %v\n", key, values)
    }
}

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

Unspotted answered 27/6, 2022 at 18:53 Comment(0)
R
0

If you know what type is come you can use

ctx.GetString("string")
ctx.GetBool("bool")
ctx.GetInt("int")
Rhapsodic answered 20/10, 2023 at 20:6 Comment(0)
C
0

If you want to get the value from the last part of the URL "api/abc/value". Use the .Param method that collects from the url. Don't forget to change your URL to: "api/abc/:value" (the 2 dots are very important)

code example:


func ReturnPersonalitie(c *gin.Context) {
    vars := c.Param("id")
    id, err := strconv.Atoi(vars)
    if err != nil {
        fmt.Println("error: ", err)
    }

    for _, personalitie := range models.Personalities {
        if personalitie.Id == id {
            c.JSON(http.StatusOK, personalitie)
        }
    }
    fmt.Printf("Value id: %d", id)
}

calling:


r.GET("/api/personalidades/:id", func(ctx *gin.Context) {
    controllers.ReturnPersonalitie(ctx)
})

For more information read: https://gin-gonic.com/docs/examples/param-in-path/

Catabolism answered 9/1 at 19:56 Comment(2)
You're right, I'll improve next time, but I wanted to share the solution I found after coming here to this question!Catabolism
ok I changed it, can you validate?Catabolism

© 2022 - 2024 — McMap. All rights reserved.