filters<\/strong> have one value which is [“color”]<\/li><\/ul>\n\n\n\nhttp:\/\/localhost:8080\/products?filters=color<\/code><\/pre>\n\n\n\nIn below example filters<\/strong> have multiple value which is [“color”, “price”, “brand”]. Notice how multiple values are defined<\/li><\/ul>\n\n\n\nhttp:\/\/localhost:8080\/products?filters=red&filters=color&filters=price&filters=brand<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
Let’s explore two methods of getting these query params<\/p>\n\n\n\n
1.Using reqeust.URL.Query()<\/strong><\/h1>\n\n\n\nThe query params lie in the URL<\/strong> itself. We get Query params via r.URL.Query() which returns Values <\/strong>which is of map[string][]string. There are two cases: <\/p>\n\n\n\n1.1 When a particular key contains multiple values in a query params. For example, see below request-<\/strong><\/p>\n\n\n\nhttp:\/\/localhost:8080\/products?filters=red&filters=color&filters=price&filters=brand<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
Code:<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n \"fmt\"\n \"net\/http\"\n \"strings\"\n)\n\nfunc main() {\n getProductsHandler := http.HandlerFunc(getProducts)\n http.Handle(\"\/products\", getProductsHandler)\n http.ListenAndServe(\":8080\", nil)\n}\n\nfunc getProducts(w http.ResponseWriter, r *http.Request) {\n query := r.URL.Query()\n filters, present := query[\"filters\"] \/\/filters=[\"color\", \"price\", \"brand\"]\n if !present || len(filters) == 0 {\n fmt.Println(\"filters not present\")\n }\n w.WriteHeader(200)\n w.Write([]byte(strings.Join(filters, \",\")))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\ncolor,price,brand\n<\/code><\/pre>\n\n\n\n1.2 When a particular key contains a single value in query params. For example, see below request<\/strong><\/p>\n\n\n\nhttp:\/\/localhost:8080\/products?filters=color<\/code><\/pre>\n\n\n\nWhen we know that a particular key has only one value in the query params, then r.URL.Query().Get(keyName) can also be used. Get function will get the first value associated with the key. If you want to all values then the map has to be directly accessed as we did in the above program. In the below program, we are using Get() on the r.URL.Query() and it returns a single value<\/p>\n\n\n\n
package main\n\nimport (\n \"net\/http\"\n)\n\nfunc main() {\n getProductsHandler := http.HandlerFunc(getProducts)\n http.Handle(\"\/products\", getProductsHandler)\n http.ListenAndServe(\":8080\", nil)\n}\n\nfunc getProducts(w http.ResponseWriter, r *http.Request) {\n query := r.URL.Query()\n filters := query.Get(\"filters\") \/\/filters=\"color\"\n w.WriteHeader(200)\n w.Write([]byte(filters))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\ncolor<\/code><\/pre>\n\n\n\n2.Using request.Form<\/strong><\/h1>\n\n\n\nThe query params lie in the URL<\/strong> itself. We get Query params via r.URL.Query() which returns values <\/strong>which is of map[string][]string. There are two cases:<\/p>\n\n\n\n2.1<\/strong> When a particular key contains multiple values in a query params. For example, see below request<\/strong><\/p>\n\n\n\nhttp:\/\/localhost:8080\/products?filters=red&filters=color&filters=price&filters=brand<\/code><\/pre>\n\n\n\nCode:<\/strong><\/p>\n\n\n\npackage main\n\nimport (\n \"fmt\"\n \"net\/http\"\n \"strings\"\n)\n\nfunc main() {\n getProductsHandler := http.HandlerFunc(getProducts)\n http.Handle(\"\/products\", getProductsHandler)\n http.ListenAndServe(\":8080\", nil)\n}\n\nfunc getProducts(w http.ResponseWriter, r *http.Request) {\n r.ParseForm()\n filters, present := r.Form[\"filters\"] \/\/filters=[\"color\", \"price\", \"brand\"]\n if !present || len(filters) == 0 {\n fmt.Println(\"filters not present\")\n }\n w.WriteHeader(200)\n w.Write([]byte(strings.Join(filters, \",\")))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\ncolor,price,brand<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
An important point to be noted:<\/p>\n\n\n\n
While trying to get query param value via r.Form , be careful that in case of POST and PUT request, body parameters will take precedence over URL query string values i.e, if there is a key say X <\/strong>present in both form body (=”a”) and in query param(=”b”). Then on calling r.Form[“X”] it will return [“a”] and not [“b”]<\/li><\/ul>\n\n\n\n2.2 When a particular key contains a single value in query params. For example, see below request<\/strong><\/p>\n\n\n\nhttp:\/\/localhost:8080\/products?filters=color<\/code><\/pre>\n\n\n\nWhen we know that a particular key has only one value in the query params, then r.FormValue(keyName) can also be used. FormValue function will get the first value associated with the key. If you want to all values then request. The form map has to be directly accessed as we did in the above program. In the below program, we are using FormValue() function and it returns a single value.<\/p>\n\n\n\n
package main\n\nimport (\n \"net\/http\"\n)\n\nfunc main() {\n getProductsHandler := http.HandlerFunc(getProducts)\n http.Handle(\"\/products\", getProductsHandler)\n http.ListenAndServe(\":8080\", nil)\n}\n\nfunc getProducts(w http.ResponseWriter, r *http.Request) {\n filters := r.FormValue(\"filters\") \/\/filters=[\"color\"]\n w.WriteHeader(200)\n w.Write([]byte(filters))\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\ncolor<\/code><\/pre>\n\n\n\nAn important point to be noted:<\/p>\n\n\n\n
While trying to get query param value via r.FormValue() , be careful that in case of POST and PUT request, body parameters will take precedence over URL query string values i.e, if there is a key say X <\/strong>present in both form body (=”a”) and in query param(=”b”). Then on calling r.Form[“X”] it will return [“a”] and not [“b”]<\/li><\/ul>\n\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"
Note: If you are interested in learning Golang, then for that we have a golang comprehensive tutorial series. Do check it out \u2013\u00a0Golang Comprehensive Tutorial Series. Now let’s see current tutorial Often…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,57,58,59],"class_list":["post-537","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-http","tag-net-http","tag-query-params"],"yoast_head":"\n
net\/http package get Query Params in Go(Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n