package main
import (
"fmt"
"net/http"
)
type StringServer string
func (s StringServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
fmt.Printf("Prior ParseForm: %v
", req.Form)
req.ParseForm()
fmt.Printf("Post ParseForm: %v
", req.Form)
fmt.Println("Param1 is : " + req.Form.Get("param1"))
fmt.Printf("PostForm : %v
", req.PostForm)
rw.Write([]byte(string(s)))
}
func createServer(addr string) http.Server {
return http.Server{
Addr: addr,
Handler: StringServer("Hello world"),
}
}
func main() {
s := createServer(":8080")
fmt.Println("Server is starting...")
if err := s.ListenAndServe(); err != nil {
panic(err)
}
}
/*
➜ recipe08 curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "param1=data1¶m2=data2" "localhost:8080?param1=overriden¶m3=data3"
Hello world
Server is starting...
Prior ParseForm: map[]
Post ParseForm: map[param1:[data1 overriden] param2:[data2] param3:[data3]]
Param1 is : data1
PostForm : map[param1:[data1] param2:[data2]]
*/