Basic Authentication in HTTP API requests in Golang
In Golang, implementing basic authentication in an HTTP API request is relatively straightforward. Once we construct the request, then we have to call the SetBasicAuth() method and pass username & password package main import ( "fmt" "io/ioutil" "log" "net/http" ) func main() { client := &http.Client{} req, err := http.NewRequest("GET", "http://google.com", nil) if err != nil { log.Fatal(err) } req.SetBasicAuth("admin", "password") resp, err := client.Do(req) if err != nil { log.Fatal(err) } defer resp.Body.Close() bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } fmt.Printf("%s\n", bodyText) } It is equivalent of, ...