diff --git a/api/api.go b/api/api.go index 08f00c406..c237b2cc6 100644 --- a/api/api.go +++ b/api/api.go @@ -314,6 +314,8 @@ type Config struct { Namespace string TLSConfig TLSConfig + + Headers map[string]string } // TLSConfig is used to generate a TLSClientConfig that's useful for talking to @@ -855,6 +857,10 @@ func (c *Client) newRequest(method, path string) *request { params: make(map[string][]string), header: make(http.Header), } + + for k, v := range c.config.Headers { + r.header.Set(k, v) + } if c.config.Datacenter != "" { r.params.Set("dc", c.config.Datacenter) } diff --git a/api/api_test.go b/api/api_test.go index d4e6dab46..94566e697 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -808,6 +808,22 @@ func TestAPI_SetWriteOptions(t *testing.T) { } } +func TestAPI_Headers(t *testing.T) { + t.Parallel() + c, s := makeClientWithConfig(t, func(c *Config) { + c.Headers = map[string]string{ + "Hello": "World", + } + }, nil) + defer s.Stop() + + r := c.newRequest("GET", "/v1/kv/foo") + + if r.header.Get("Hello") != "World" { + t.Fatalf("bad: %v", r.header) + } +} + func TestAPI_RequestToHTTP(t *testing.T) { t.Parallel() c, s := makeClient(t)