Golang : Set and Get HTTP request headers example
Couple of examples on how to set and get HTTP request headers in Golang.
Example of setting HTTP request header.
To set User-Agent for your Golang application, first create a client and set client's request header with [your user-agent name] before executing the request.
For instance :
client := &http.Client{}
request, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
log.Fatalln(err)
}
request.Header.Set("User-Agent", "[your user-agent name]")
resp, err := client.Do(req)
Example of getting file download size
response, err := http.Head(url)
if err != nil {
log.Println("Error while downloading", url, ":", err)
}
length, _ := strconv.Atoi(response.Header.Get("Content-Length"))
sourceSize := int64(length)
Example of getting HTTP request header User-Agent information with Header.Get()
method
package main
import (
"fmt"
"net/http"
)
func getUserAgent(w http.ResponseWriter, r *http.Request) {
ua := r.Header.Get("User-Agent")
fmt.Printf("user agent is: %s \n", ua)
w.Write([]byte("user agent is " + ua))
}
func main() {
http.HandleFunc("/", getUserAgent)
http.ListenAndServe(":8080", nil)
}
Apart from User-Agent, here is a list of common HTTP headers :
"accept"
"accept-charset"
"accept-encoding"
"accept-language"
"accept-ranges"
"age"
"access-control-allow-origin"
"allow"
"authorization"
"cache-control"
"content-disposition"
"content-encoding"
"content-language"
"content-length"
"content-location"
"content-range"
"content-type"
"cookie"
"date"
"etag"
"expect"
"expires"
"from"
"host"
"if-match"
"if-modified-since"
"if-none-match"
"if-unmodified-since"
"last-modified"
"link"
"location"
"max-forwards"
"proxy-authenticate"
"proxy-authorization"
"range"
"referrer"
"refresh"
"retry-after"
"server"
"set-cookie"
"strict-transport-security"
"transfer-encoding"
"user-agent"
"vary"
"via"
"www-authenticate"
References :
https://www.socketloop.com/references/golang-net-http-canonicaheaderkey-function-example
https://www.socketloop.com/tutorials/golang-how-to-get-http-request-header-information
https://www.socketloop.com/tutorials/golang-how-to-set-or-add-header-http-responsewriter
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+3.5k Java : Random alphabets, alpha-numeric or numbers only string generator
+15.6k Golang : Update database with GORM example
+14.2k Golang : On enumeration
+6.7k Golang : Gargish-English language translator
+10.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+9.4k Golang : Detect number of active displays and the display's resolution
+21.2k Golang : GORM create record or insert new record into database example
+5.8k Linux/MacOSX : Search for files by filename and extension with find command
+9.9k Golang : Compare files modify date example
+5.1k Unix/Linux/MacOSx : How to remove an environment variable ?
+4.8k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+24.7k Golang : Create PDF file from HTML file