diff --git a/api/api.go b/api/api.go index e54d37bb9..bb553b645 100644 --- a/api/api.go +++ b/api/api.go @@ -155,6 +155,8 @@ type Config struct { // // TLSConfig is ignored if HttpClient is set. TLSConfig *TLSConfig + + Headers http.Header } // ClientConfig copies the configuration with a new client address, region, and @@ -527,6 +529,7 @@ type request struct { body io.Reader obj interface{} ctx context.Context + header http.Header } // setQueryOptions is used to annotate the request with @@ -612,6 +615,8 @@ func (r *request) toHTTP() (*http.Request, error) { return nil, err } + req.Header = r.header + // Optionally configure HTTP basic authentication if r.url.User != nil { username := r.url.User.Username() @@ -649,6 +654,7 @@ func (c *Client) newRequest(method, path string) (*request, error) { Path: u.Path, RawPath: u.RawPath, }, + header: make(http.Header), params: make(map[string][]string), } if c.config.Region != "" { @@ -671,6 +677,10 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } + if c.config.Headers != nil { + r.header = c.config.Headers + } + return r, nil } diff --git a/api/api_test.go b/api/api_test.go index d46098796..0048ecdfe 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -341,6 +341,22 @@ func TestParseWriteMeta(t *testing.T) { } } +func TestClientHeader(t *testing.T) { + t.Parallel() + c, s := makeClient(t, func(c *Config) { + c.Headers = http.Header{ + "Hello": []string{"World"}, + } + }, nil) + defer s.Stop() + + r, _ := c.newRequest("GET", "/v1/jobs") + + if r.header.Get("Hello") != "World" { + t.Fatalf("bad: %v", r.header) + } +} + func TestQueryString(t *testing.T) { t.Parallel() c, s := makeClient(t, nil, nil) diff --git a/vendor/github.com/hashicorp/nomad/api/api.go b/vendor/github.com/hashicorp/nomad/api/api.go index e54d37bb9..bb553b645 100644 --- a/vendor/github.com/hashicorp/nomad/api/api.go +++ b/vendor/github.com/hashicorp/nomad/api/api.go @@ -155,6 +155,8 @@ type Config struct { // // TLSConfig is ignored if HttpClient is set. TLSConfig *TLSConfig + + Headers http.Header } // ClientConfig copies the configuration with a new client address, region, and @@ -527,6 +529,7 @@ type request struct { body io.Reader obj interface{} ctx context.Context + header http.Header } // setQueryOptions is used to annotate the request with @@ -612,6 +615,8 @@ func (r *request) toHTTP() (*http.Request, error) { return nil, err } + req.Header = r.header + // Optionally configure HTTP basic authentication if r.url.User != nil { username := r.url.User.Username() @@ -649,6 +654,7 @@ func (c *Client) newRequest(method, path string) (*request, error) { Path: u.Path, RawPath: u.RawPath, }, + header: make(http.Header), params: make(map[string][]string), } if c.config.Region != "" { @@ -671,6 +677,10 @@ func (c *Client) newRequest(method, path string) (*request, error) { } } + if c.config.Headers != nil { + r.header = c.config.Headers + } + return r, nil }