From ac5248007e1b3f065d4903f42e36d4e6d0c44181 Mon Sep 17 00:00:00 2001 From: Andrea Sosso Date: Tue, 26 Jan 2016 14:18:22 +0100 Subject: [PATCH] Additional request header parameters for httpjson plugin --- plugins/inputs/httpjson/README.md | 10 ++++++++++ plugins/inputs/httpjson/httpjson.go | 12 ++++++++++++ plugins/inputs/httpjson/httpjson_test.go | 8 ++++++++ 3 files changed, 30 insertions(+) diff --git a/plugins/inputs/httpjson/README.md b/plugins/inputs/httpjson/README.md index d016633a7..fc45dd567 100644 --- a/plugins/inputs/httpjson/README.md +++ b/plugins/inputs/httpjson/README.md @@ -45,6 +45,16 @@ You can also specify additional request parameters for the service: ``` +You can also specify additional request header parameters for the service: + +``` +[[httpjson.services]] + ... + + [httpjson.services.headers] + X-Auth-Token = "my-xauth-token" + apiVersion = "v1" +``` # Example: diff --git a/plugins/inputs/httpjson/httpjson.go b/plugins/inputs/httpjson/httpjson.go index 577b6922e..bc4fe7899 100644 --- a/plugins/inputs/httpjson/httpjson.go +++ b/plugins/inputs/httpjson/httpjson.go @@ -21,6 +21,7 @@ type HttpJson struct { Method string TagKeys []string Parameters map[string]string + Headers map[string]string client HTTPClient } @@ -70,6 +71,12 @@ var sampleConfig = ` [inputs.httpjson.parameters] event_type = "cpu_spike" threshold = "0.75" + + # HTTP Header parameters (all values must be strings) + # [inputs.httpjson.headers] + # X-Auth-Token = "my-xauth-token" + # apiVersion = "v1" + ` func (h *HttpJson) SampleConfig() string { @@ -191,6 +198,11 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) { return "", -1, err } + // Add header parameters + for k, v := range h.Headers { + req.Header.Add(k, v) + } + start := time.Now() resp, err := h.client.MakeRequest(req) if err != nil { diff --git a/plugins/inputs/httpjson/httpjson_test.go b/plugins/inputs/httpjson/httpjson_test.go index 0ea5e9e42..9eff03887 100644 --- a/plugins/inputs/httpjson/httpjson_test.go +++ b/plugins/inputs/httpjson/httpjson_test.go @@ -97,6 +97,10 @@ func genMockHttpJson(response string, statusCode int) []*HttpJson { "httpParam1": "12", "httpParam2": "the second parameter", }, + Headers: map[string]string{ + "X-Auth-Token": "the-first-parameter", + "apiVersion": "v1", + }, }, &HttpJson{ client: mockHTTPClient{responseBody: response, statusCode: statusCode}, @@ -110,6 +114,10 @@ func genMockHttpJson(response string, statusCode int) []*HttpJson { "httpParam1": "12", "httpParam2": "the second parameter", }, + Headers: map[string]string{ + "X-Auth-Token": "the-first-parameter", + "apiVersion": "v1", + }, TagKeys: []string{ "role", "build",