[Golang] Server Get Form POST Value


Server get HTML form value from HTTP POST in Golang. Use PostFormValue method in net/http package.

read.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
	"fmt"
	"net/http"
)

var indexHtml = `<!doctype html>
<html>
<head><title>Get Form POST Value</title></head>
<body>
<form action="/post" method="post">
  <input name="myValue">
  <button>Send</button>
</form>
</body>
</html>`

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, indexHtml)
}

func postHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, r.PostFormValue("myValue"))
}

func main() {
	http.HandleFunc("/", handler)
	http.HandleFunc("/post", postHandler)
	http.ListenAndServe(":8000", nil)
}

Tested on: Ubuntu Linux 15.10, Go 1.6.


References:

[1]http - The Go Programming Language
[2]Don’t parse everything from client multipart POST : golang
[3]Realtime applications with GO on backend side : golang