9951 explained code solutions for 126 technologies


golangHow to set user agent for HTTP request


package main

import ("net/http"; "os"; "io")

func main() {
  client := &http.Client{}
  req, _ := http.NewRequest("GET", "https://echoof.me", nil)
  req.Header.Set("User-Agent", "super-go-client")
  r, _ := client.Do(req)
  defer r.Body.Close()
  io.Copy(os.Stdout, r.Body)
}ctrl + c
package main

default package declaration

net/http

http package to work with http protocol

http.Client{}

creates new HTTP client object

http.NewRequest

creates HTTP request object

.Header.Set

set custom header (can be executed multiple times)

User-Agent

header allows to set user agent

super-go-client

sample value for user agent to set

.Do(

sends given request

io.Copy(os.Stdout, r.Body)

output response body to stdout