Creating an HTTP request
This recipe will show you how to construct a HTTP request with specific parameters.
How to do it...
- Open the console and create the folder
chapter07/recipe06. - Navigate to the directory.
- Create the
request.gofile with the following content:
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
type StringServer string
func (s StringServer) ServeHTTP(rw http.ResponseWriter,
req *http.Request) {
req.ParseForm()
fmt.Printf("Received form data: %v\n", req.Form)
fmt.Printf("Received header: %v\n", req.Header)
rw.Write([]byte(string(s)))
}
func createServer(addr string) http.Server {
return http.Server{
Addr: addr,
Handler: StringServer("Hello world"),
}
}
const addr = "localhost:7070"
func main() ...