9951 explained code solutions for 126 technologies

golangHow to set custom headers 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("my-header", "hi from onelinerhub")
  r, _ := client.Do(req)
  defer r.Body.Close()
  io.Copy(os.Stdout, r.Body)
}ctrl + c
package main

default package declaration


http package to work with http protocol


creates new HTTP client object


creates HTTP request object


set custom header (can be executed multiple times)


header name

"hi from onelinerhub"

header value


sends given request

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

output response body to stdout