Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

access post parameters in handler

Tags:

go

mux

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
}
like image 395
Maxim Yefremov Avatar asked Jan 28 '15 12:01

Maxim Yefremov


People also ask

How to access model parameters after handling a request?

Method postHandle () After handling a request, our model parameters are available, so we may access them to change values or add new ones. In order to do that, we use the overridden postHandle () method: Let's take a look at the implementation details. First of all, it's better to check if the model is not null.

Where are the parameters sent in an HTTP POST request?

In an HTTP POST request, the parameters are not sent along with the URI. Where are the values? In the request header? In the request body? What does it look like? Show activity on this post. The values are sent in the request body, in the format that the content type specifies.

What is the status parameter for the event handler?

Every event handler has a status parameter that controls the event handler. For Complete events, this parameter is also used to indicate the success or failure of the operation that generated the event.

How do I pass a parameter to a form field?

In a POST handler, the parameter name must match a form field name for the incoming value to be automatically bound to the parameter: Alternatively, you can use the form tag helper's asp-route attribute to pass parameter values as part of the URL, either as a query string value or as route data for GET requests:


1 Answers

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

func logonPost(w http.ResponseWriter, r *http.Request) {
    login := r.FormValue("login")
    // ...
}
like image 92
Ainar-G Avatar answered Sep 21 '22 09:09

Ainar-G