diff --git a/plugins/outputs/http/http.go b/plugins/outputs/http/http.go index e36460ac8..91c2954cd 100644 --- a/plugins/outputs/http/http.go +++ b/plugins/outputs/http/http.go @@ -131,6 +131,10 @@ func (h *HTTP) write(reqBody []byte) error { return err } + if h.Username != "" || h.Password != "" { + req.SetBasicAuth(h.Username, h.Password) + } + req.Header.Set("Content-Type", defaultContentType) for k, v := range h.Headers { req.Header.Set(k, v) diff --git a/plugins/outputs/http/http_test.go b/plugins/outputs/http/http_test.go index 1d511d85b..daec176be 100644 --- a/plugins/outputs/http/http_test.go +++ b/plugins/outputs/http/http_test.go @@ -235,10 +235,8 @@ func TestBasicAuth(t *testing.T) { require.NoError(t, err) tests := []struct { - name string - plugin *HTTP - username string - password string + name string + plugin *HTTP }{ { name: "default", @@ -274,8 +272,8 @@ func TestBasicAuth(t *testing.T) { t.Run(tt.name, func(t *testing.T) { ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { username, password, _ := r.BasicAuth() - require.Equal(t, tt.username, username) - require.Equal(t, tt.password, password) + require.Equal(t, tt.plugin.Username, username) + require.Equal(t, tt.plugin.Password, password) w.WriteHeader(http.StatusOK) })