Skip to content

Instantly share code, notes, and snippets.

@rgarcia
Created June 11, 2014 17:27
Show Gist options
  • Save rgarcia/e0df844fdad92352bb22 to your computer and use it in GitHub Desktop.
Save rgarcia/e0df844fdad92352bb22 to your computer and use it in GitHub Desktop.
golang basic auth transport
import (
"encoding/base64"
"fmt"
"net/http"
)
type BasicAuthTransport struct {
Username string
Password string
}
func (bat BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", fmt.Sprintf("Basic %s",
base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s",
bat.Username, bat.Password)))))
return http.DefaultTransport.RoundTrip(req)
}
func (bat *BasicAuthTransport) Client() *http.Client {
return &http.Client{Transport: bat}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment