Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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         }     } } 
like image 225
codec Avatar asked Dec 22 '16 09:12

codec


People also ask

What is Gin Default ()?

gin. Default() creates a Gin router with default middleware: logger and recovery middleware. Next, we make a handler using router. GET(path, handle) , where path is the relative path,​ and handle is the handler function that takes *gin. Context as an argument.

What is gin context?

The gin Context is a structure that contains both the http. Request and the http. Response that a normal http. Handler would use, plus some useful methods and shortcuts to manipulate those. The gin engine is responsible for the creation (and reuse) of those contexts, in the same manner as the http.

What are query parameters in URL?

Query parameters are a defined set of parameters attached to the end of a url. They are extensions of the URL that are used to help define specific content or actions based on the data being passed. To append query params to the end of a URL, a '? ' Is added followed immediately by a query parameter.


1 Answers

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

like image 146
ctcherry Avatar answered Oct 19 '22 01:10

ctcherry