access post parameters in handler

Maxim Yefremov picture Maxim Yefremov · Jan 28, 2015 · Viewed 14k times · Source

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
}

Answer

Ainar-G picture Ainar-G · Jan 28, 2015

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

func logonPost(w http.ResponseWriter, r *http.Request) {
    login := r.FormValue("login")
    // ...
}