-
-
Save caitong93/1c9c7ee9abbae1996e27f72b113d8f5a to your computer and use it in GitHub Desktop.
golang basic auth transport
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