Essential Go HTTP Client  Edit on GitHub      File Issue

Set User-Agent

An HTTP requests includes User-Agent header.

It’s a good idea to use user agent meaningful for your program.

Also, some servers are fussy about what they accept.

This is how to set custom user agent:

userAgent := "Special Agent v 1.0.16"

client := &http.Client{}
client.Timeout = time.Second * 15

uri := ""
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
	log.Fatalf("http.NewRequest() failed with '%s'\n", err)
req.Header.Set("User-Agent", userAgent)
resp, err := client.Do(req)
if err != nil {
	log.Fatalf("client.Do() failed with '%s'\n", err)

defer resp.Body.Close()
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
	log.Fatalf("ioutil.ReadAll() failed with '%s'\n", err)

fmt.Printf("Response status code: %d, text:\n%s\n", resp.StatusCode, string(d))
Response status code: 200, text:
  "user-agent": "Special Agent v 1.0.16"
  ↑ ↓ to navigate     ↵ to select     Esc to close