Created
June 11, 2014 17:27
-
-
Save rgarcia/e0df844fdad92352bb22 to your computer and use it in GitHub Desktop.
golang basic auth transport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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