access post parameters in handler
Asked Answered
A

1

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
}
Andress answered 28/1, 2015 at 12:23 Comment(0)
N
32

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

func logonPost(w http.ResponseWriter, r *http.Request) {
    login := r.FormValue("login")
    // ...
}
Nilgai answered 28/1, 2015 at 12:38 Comment(2)
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.Passe
@Passe No, this is about a usual HTML form. For JSON you need to decode a structure from a body.Nilgai

© 2022 - 2024 — McMap. All rights reserved.