Reading and writing HTTP headers
The previous recipe describes how you can create a HTTP request in general. This recipe will go into detail on how to read and write request headers.
How to do it...
- Open the console and create the folder
chapter07/recipe07
. - Navigate to the directory.
- Create the
headers.go
file with the following content:
package main import ( "fmt" "net/http" ) func main() { header := http.Header{} // Using the header as slice header.Set("Auth-X", "abcdef1234") header.Add("Auth-X", "defghijkl") fmt.Println(header) // retrieving slice of values in header resSlice := header["Auth-X"] fmt.Println(resSlice) // get the first value resFirst := header.Get("Auth-X") fmt.Println(resFirst) // replace all existing values with // this one header.Set("Auth-X", "newvalue") fmt.Println...