From 54405ab2206ea16d048dfc116a96f875c1809af8 Mon Sep 17 00:00:00 2001 From: Andrea Sosso Date: Wed, 30 Dec 2015 10:26:36 +0100 Subject: [PATCH] Additional request header parameters for the service --- plugins/httpjson/README.md | 12 ++++++++++++ plugins/httpjson/httpjson.go | 12 ++++++++++++ plugins/httpjson/httpjson_test.go | 8 ++++++++ 3 files changed, 32 insertions(+) diff --git a/plugins/httpjson/README.md b/plugins/httpjson/README.md index d016633a7134d..c75a43fc394e2 100644 --- a/plugins/httpjson/README.md +++ b/plugins/httpjson/README.md @@ -45,6 +45,18 @@ 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/httpjson/httpjson.go b/plugins/httpjson/httpjson.go index f1d2ef9275390..0a51b36209cee 100644 --- a/plugins/httpjson/httpjson.go +++ b/plugins/httpjson/httpjson.go @@ -24,6 +24,7 @@ type Service struct { Method string TagKeys []string Parameters map[string]string + Headers map[string]string } type HTTPClient interface { @@ -72,6 +73,11 @@ var sampleConfig = ` [plugins.httpjson.services.parameters] event_type = "cpu_spike" threshold = "0.75" + + # HTTP Header parameters (all values must be strings) + # [plugins.httpjson.services.headers] + # X-Auth-Token = "my-xauth-token" + # apiVersion = "v1" ` func (h *HttpJson) SampleConfig() string { @@ -176,6 +182,7 @@ func (h *HttpJson) sendRequest(service Service, serverURL string) (string, error for k, v := range service.Parameters { params.Add(k, v) } + requestURL.RawQuery = params.Encode() // Create + send request @@ -184,6 +191,11 @@ func (h *HttpJson) sendRequest(service Service, serverURL string) (string, error return "", err } + // Add header parameters + for k, v := range service.Headers { + req.Header.Add(k, v) + } + resp, err := h.client.MakeRequest(req) if err != nil { return "", err diff --git a/plugins/httpjson/httpjson_test.go b/plugins/httpjson/httpjson_test.go index 8f9bfe3ac2e85..c64c326f5a9ce 100644 --- a/plugins/httpjson/httpjson_test.go +++ b/plugins/httpjson/httpjson_test.go @@ -91,6 +91,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", + }, }, Service{ Servers: []string{ @@ -103,6 +107,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",