Set User-Agent header

It's nice to third parties if you set User-Agent so the caller can be
identified. By default Go will set a User-Agent header to a string like
"Go-http-client/1.1".

The library now sets it to a string that identifies the source of the
program (github.com/jamesog/aaisp-chaos) and the compiled Go OS, arch
and version, to aid any potential debugging:

    chaos-go (darwin; amd64; go1.15.5) github.com/jamesog/aaisp-chaos
This commit is contained in:
James O'Gorman
2020-11-28 15:56:16 +00:00
parent 16d476249d
commit 89bacd6ee3

View File

@@ -8,6 +8,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
"runtime"
"strings"
"time"
)
@@ -55,6 +56,14 @@ func (a Auth) form() url.Values {
return f
}
func userAgent() string {
return fmt.Sprintf("chaos-go (%s; %s; %s) github.com/jamesog/aaisp-chaos",
runtime.GOOS,
runtime.GOARCH,
runtime.Version(),
)
}
func (api API) makeRequest(url string) ([]byte, error) {
client := &http.Client{
Timeout: 10 * time.Second,
@@ -64,6 +73,7 @@ func (api API) makeRequest(url string) ([]byte, error) {
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent())
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {