Created
February 19, 2017 07:57
-
-
Save DavadDi/137a649f042bcee2fdcd19d2650bfe6a to your computer and use it in GitHub Desktop.
http basic auth
This file contains hidden or 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