2825 explained code solutions for 75 technologies


golangHow to use basic auth with HTTP client


package main

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

func main() {
  h := http.Client{}
  req, _ := http.NewRequest("GET", "https://echoof.me/", nil)
  req.SetBasicAuth("user", "pwd")
  r, _ := h.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

"GET"

HTTP method to use

SetBasicAuth(

set basic auth username and password

.Do(

sends given request

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

output response body to stdout