Forms play a major role in a website, whenever we want to collect data from the user and want to process it on our server. You already have seen so many websites where you need to login and register, all those websites implement HTML Forms and the data entered is stored on the server. We will also implement Golang Form Data and also learn to parse and read the Form.
In this blog, we will First create an HTML Form and then learn how we can read GET and POST data from HTML Forms using Golang Request Body.
Golang Template
In this HTML template, we are using the Bootstrap starter template so that we don’t have to design the whole form as our primary focus is to learn how we can read data from forms using Golang.
<div class="container">
<form>
<div class="form-group">
<label for="InputEmail1">Email address</label>
<input type="email" class="form-control" id="InputEmail1" name="Email" aria-describedby="emailHelp">
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone
else.</small>
</div>
<div class="form-group">
<label for="InputPassword1">Password</label>
<input type="password" class="form-control" name="password" id="InputPassword1">
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" name="remember_check" id="Check1">
<label class="form-check-label" for="Check1">Remember Me</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
HTML
Learn about Templates in Golang as it will be useful for this blog.
This is how the form looks:
Now we will have to implement the idea that we should be able to get the data from the form whenever the user clicks the submit button.
Golang Request GET Form Data
First of all the Form is parsed using the ParseForm Method.
func login(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
fmt.Fprintln(w, "Email : ", r.Form.Get("email"))
fmt.Fprintln(w, "Password : ", r.Form.Get("password"))
fmt.Fprintln(w, "Remember Me : ", r.Form.Get("remember_check"))
}
Go
For data sent using the GET Method, the Form Method is used to read data from the parsed form in the request.
Golang POST Form Data
func login(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
fmt.Fprintln(w, "Email : ", r.PostForm.Get("email"))
fmt.Fprintln(w, "Password : ", r.PostForm.Get("password"))
fmt.Fprintln(w, "Remember Me : ", r.PostForm.Get("remember_check"))
}
Go
We Use PostForm for the data sent using the POST Method from the HTML forms.
When the form data is sent using the GET method, the data is sent through the URL but when POST Method is used the data is sent using the body of the request.
Golang HTML Form Submit
package main
import (
"fmt"
"html/template"
"log"
"net/http"
)
func renderTemplate(w http.ResponseWriter, r *http.Request) {
parsedTemplate, _ := template.ParseFiles("Template/index.html")
err := parsedTemplate.Execute(w, nil)
if err != nil {
log.Println("Error executing template :", err)
return
}
}
func login(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
fmt.Fprintln(w, "Email : ", r.PostForm.Get("email"))
fmt.Fprintln(w, "Password : ", r.PostForm.Get("password"))
fmt.Fprintln(w, "Remember Me : ", r.PostForm.Get("remember_check"))
}
func main() {
http.HandleFunc("/", renderTemplate)
http.HandleFunc("/login", login)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Error Starting the HTTP Server : ", err)
return
}
}
Go
Learn more about Golang Forms from Golang.org.