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

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)

"my-header"

header name

"hi from onelinerhub"

header value

.Do(

sends given request

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

output response body to stdout


Usage example

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)
}
output
IP:                           135.181.98.214

ACCEPT_ENCODING:              gzip
MY_HEADER:                    hi from onelinerhub
USER_AGENT:                   Go-http-client/1.1

https://echoof.me