From 89bacd6ee383f64d3913771c28c44d74c68def6b Mon Sep 17 00:00:00 2001 From: James O'Gorman Date: Sat, 28 Nov 2020 15:56:16 +0000 Subject: [PATCH] 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 --- chaos.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/chaos.go b/chaos.go index 016f39c..0908435 100644 --- a/chaos.go +++ b/chaos.go @@ -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 {