Reading your first HTML form
Once an HTML form is submitted, we have to read the client data on the server side to take an appropriate action. We will be covering this in this recipe.
Getting ready…
Since we have already created an HTML form in our previous recipe, we will just extend the recipe to read its field values.
How to do it…
- Install the
github.com/gorilla/schema
package using thego get
command, as follows:
$ go get github.com/gorilla/schema
- Create
html-form-read.go
, where we will read an HTML form field after decoding it using thegithub.com/gorilla/schema
package and writeHello
followed by the username to an HTTP response stream, as follows:
package main import ( "fmt" "html/template" "log" "net/http" "github.com/gorilla/schema" ) const ( CONN_HOST = "localhost" CONN_PORT = "8080" ) type User struct { Username string Password string } func readForm(r *http.Request) *User { r.ParseForm() user := new(User) decoder := schema.NewDecoder() decodeErr := decoder...