Golang HTTP POST handling

99 views
Skip to first unread message

Sankar

unread,
Feb 20, 2018, 11:16:57 AM2/20/18
to golang-nuts
Hi,

I have a Golang HTTP server which is consumed by mobile Apps via HTTP. 

For one workflow (reset-password) alone, I needed a web interface. So instead of creating a new different webapp, I decided to make an endpoint emit text/html instead of JSON and then serve a static HTML page, which will then do a POST request. 

The same endpoint will serve HTML on HTTP GET and will process the values in HTTP POST.

However, I am not able to get the POST parameters in my HTTP request. Even after a succesful req.ParseForm, my req.Form returns nil.

I have attached the complete example.go file in the file, which you can run offline. The relevant code section is:

func alterPasswordHandler(res http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodGet {
body := `
<html>
<body>
<form method="post">
<label for="password">Password</label>
<input id="password" type="password" placeholder="Password" required>
<label for="repeatPassword">Repeat Password</label>
<input id="repeatPassword" type="password" placeholder="Repeat Password" required>
<button type="submit">Change Password</button>
</form>
</body>
</html>
`
res.WriteHeader(http.StatusOK)
res.Header().Set("Content-Type", "text/html")
res.Write([]byte(body))
} else if req.Method == http.MethodPost {
err := req.ParseForm()
log.Println(err, req.Form)                  // <<<<<<<<<<< req.Form is nil
}
}

What am I doing wrong ? I do get the POST request but without the password fields as part of the POST body. Any help ?

Thanks.
example.go
Message has been deleted

matthe...@gmail.com

unread,
Feb 20, 2018, 11:30:04 AM2/20/18
to golang-nuts
This worked for me in a project. My form is <form action=“” method=“post”> and the input is <input type=“password” name=“password”/>. On the server I use r.ParseForm then r.FormValue("password").

Matt

Sankar P

unread,
Feb 20, 2018, 11:34:47 AM2/20/18
to matthe...@gmail.com, golang-nuts
Perfect. Thanks. This is what I wanted. I just replaced id with name and it started working.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/HxLT6NlzKlY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Andrew Watson

unread,
Feb 20, 2018, 6:01:09 PM2/20/18
to golang-nuts
You should, of course, consider adding some rigor such as CSRF protection to that form! I'd suggest something like http://www.gorillatoolkit.org/pkg/csrf for that! 

Sankar P

unread,
Feb 20, 2018, 10:21:49 PM2/20/18
to Andrew Watson, golang-nuts
Thanks. Will do.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/HxLT6NlzKlY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages