19

I can access GET parameters using mux:

import (
    "github.com/gorilla/mux"
)
func main(){
     rtr := mux.NewRouter()
     rtr.HandleFunc("/logon", logonGet).Methods("GET")
}
func logonGet(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)
    login := params["login"]
}

But cannot figure out how to access POST params

func main(){
     rtr := mux.NewRouter()
     rtr.HandleFunc("/logon", logonPost).Methods("POST")
}
func logonPost(w http.ResponseWriter, r *http.Request) {
    // how to get POST parameters from request
}
Maxim Yefremov
  • 13,671
  • 27
  • 117
  • 166

1 Answers1

32

By using (*http.Request).FormValue method.

func logonPost(w http.ResponseWriter, r *http.Request) {
    login := r.FormValue("login")
    // ...
}
Ainar-G
  • 34,563
  • 13
  • 93
  • 119
  • Does this refer to the JSON body or where exactly is the `login` field that's being retrieved? I'm trying this method but `FormValue` always just returns an empty string. – nburk Nov 27 '18 at 16:56
  • 1
    @nburk No, this is about a usual HTML form. For JSON you need to decode a structure from a body. – Ainar-G Nov 27 '18 at 17:12