You can get the query parameters from the request URL inside your route handler using the Golang net/http
package.
In your route handler function, you have access to the http.Request
object. It has a r.URL.Query()
method that retrieves a map of query parameters.
func productsHandler(w http.ResponseWriter, r *http.Request) {
// Parse the query parameters from the request
queryParams := r.URL.Query()
// Access individual query parameters by key
sort := queryParams.Get("sort")
page := queryParams.Get("page")
fmt.Println(sort)
fmt.Println(page)
fmt.Fprintln(w, "List of products")
}
Now you can call the Get()
method on it with a key to get an individual query param.
Get Query Parameters Using net/http Package in Golang
Suppose you have a /api/products
route in your Go server that returns a list of all products. But you don't want to send all products at once.
Instead, you can divide your products into different sections. You also want to add a system so that you can sort the product list into different orders.
You can easily do all of these using query parameters. You can get the query parameter values and modify the response based on that.
https://example.com/api/products?page=2&sort=asc
By adding page=2
and sort=asc
query parameters, you can specify which product section you want and in which order.
You can get the page
and sort
query parameter values inside your Go route from the http.Request
object.
package main
import (
"fmt"
"net/http"
)
func productsHandler(w http.ResponseWriter, r *http.Request) {
// Parse the query parameters from the request
queryParams := r.URL.Query()
// Access individual query parameters by key
sort := queryParams.Get("sort")
page := queryParams.Get("page")
fmt.Println(sort)
fmt.Println(page)
fmt.Fprintln(w, "List of products")
}
func main() {
http.HandleFunc("/products", productsHandler)
fmt.Println("Server started on port 8000")
http.ListenAndServe(":8000", nil)
}
In your route handler function, call r.URL.Query()
method. It will return a map with all the query params from the request path.
Now you can call the Get()
method on the map with a query parameter name to get its value.
If you pass a query parameter name that doesn't exist in the request URL, it will return an empty string.
You can use these sort
and page
query params to get data from your database and send the response as JSON data from your route.
Get All Query Params in Golang Routes
To get all the query parameters from an HTTP request in Go, you can use the r.URL.Query()
method, which returns a url.Values
type.
The url.Values
type is essentially a map where each key contains a list of values (slice of strings).
func productsHandler(w http.ResponseWriter, r *http.Request) {
// Parse the query parameters from the request
queryParams := r.URL.Query()
// Print all query parameters
for key, values := range queryParams {
for _, value := range values {
fmt.Printf("Query Param: %s = %s\n", key, value)
}
}
fmt.Fprintln(w, "List of products")
}
Now we can loop through the map to access each key and its values. As each key contains a slice of strings, we can also loop through these values.
In this way, you can access all the query parameters from a request URL in Golang routes using the net/http
package.
Also Read: How to Get Dynamic Params in Golang Routes Using net/http