Golang net/http.Header type and Add, Set, Del, Get methods examples
package net/http
Golang net/http.Header type and methods usage example
Example 1:
client := &http.Client{}
req, _ := http.NewRequest("POST", postStr, nil)
req.Header.Add("Content-Type: ", "application/x-www-form-urlencoded") // create a new key
req.Header.Set("Content-Type: ", "application/x-www-form-urlencoded") // update the existing key
req.Header.Del("Content-Type: ")
str := req.Header.Get("Content-Type: ")
resp, err := client.Do(req)
Example 2:
func addHeaderHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Methods", "PUT")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
}
References :
http://golang.org/pkg/net/http/#Header.Add
http://golang.org/pkg/net/http/#Header.Del
Advertisement
Something interesting
Tutorials
+18.7k Golang : convert int to string
+10.2k Golang : Use regular expression to get all upper case or lower case characters example
+14.1k Golang : Check if a file exist or not
+14.2k Golang : syscall.Socket example
+7.1k Golang : Gorrila mux.Vars() function example
+7.9k Golang Hello World Example
+14.5k Golang : Overwrite previous output with count down timer
+11.3k Golang : Post data with url.Values{}
+14.5k Golang : Rename directory
+13.5k Golang : How to get year, month and day?
+6.2k Golang : Extract XML attribute data with attr field tag example
+51.4k Golang : Check if item is in slice/array