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 maindefault package declaration | net/httphttp package to work with http protocol | 
| http.Client{}creates new HTTP client object | http.NewRequestcreates HTTP request object | 
| .Header.Setset 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.meRelated
- How to make POST (form data) request using HTTP client
- How to use proxy with HTTP client
- How to set user agent for HTTP request
- HTTP client example
- How to get JSON data from HTTP request
- How to set cookie for HTTP request
- How to get response status code of HTTP request
- How to use basic auth with HTTP client
- How to get response body of HTTP request
More of Golang
- How to generate random float
- How to generate random int
- Unrmarshal example
- How to iterate over a slice in reverse order
- How to sleep for 500 milliseconds
- How to sleep for 5 seconds
- How to make POST (form data) request using HTTP client
- How to use proxy with HTTP client
- Find the nameserver records of a domain name
- How to set user agent for HTTP request
See more codes...