How to get an optional request in a GET request using Gorilla Mux?

I would like some of my query parameters to be optional. At the moment I have

r.HandleFunc("/user", userByValueHandler).
    Queries(
        "username", "{username}",
        "email", "{email}",
    ).
    Methods("GET")

But in this case, the request must contain "username" and "email". I want to have a more flexible choice: there are 2 of them OR have only one of them (but not zero parameters).

Thank!

+3
source share
1 answer

So, I found a solution to rewrite my logic as:

r.HandleFunc("/user", UserByValueHandler).Methods("GET")

And in UserByValueHandlerwe can have something like:

func UserByValueHandler(w http.ResponseWriter, r *http.Request) {
       v := r.URL.Query()

       username := v.Get("username")
       email := v.Get("email")
       .....
}
+6
source

Source: https://habr.com/ru/post/1682517/


All Articles