Skip to content

Commit f8930b9

Browse files
Andrea Sossosparrc
authored andcommitted
Additional request header parameters for httpjson plugin
closes #471
1 parent c10227a commit f8930b9

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

plugins/inputs/httpjson/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ You can also specify additional request parameters for the service:
4545
4646
```
4747

48+
You can also specify additional request header parameters for the service:
49+
50+
```
51+
[[httpjson.services]]
52+
...
53+
54+
[httpjson.services.headers]
55+
X-Auth-Token = "my-xauth-token"
56+
apiVersion = "v1"
57+
```
4858

4959
# Example:
5060

plugins/inputs/httpjson/httpjson.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type HttpJson struct {
2121
Method string
2222
TagKeys []string
2323
Parameters map[string]string
24+
Headers map[string]string
2425
client HTTPClient
2526
}
2627

@@ -70,6 +71,12 @@ var sampleConfig = `
7071
[inputs.httpjson.parameters]
7172
event_type = "cpu_spike"
7273
threshold = "0.75"
74+
75+
# HTTP Header parameters (all values must be strings)
76+
# [inputs.httpjson.headers]
77+
# X-Auth-Token = "my-xauth-token"
78+
# apiVersion = "v1"
79+
7380
`
7481

7582
func (h *HttpJson) SampleConfig() string {
@@ -191,6 +198,11 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
191198
return "", -1, err
192199
}
193200

201+
// Add header parameters
202+
for k, v := range h.Headers {
203+
req.Header.Add(k, v)
204+
}
205+
194206
start := time.Now()
195207
resp, err := h.client.MakeRequest(req)
196208
if err != nil {

plugins/inputs/httpjson/httpjson_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ func genMockHttpJson(response string, statusCode int) []*HttpJson {
9797
"httpParam1": "12",
9898
"httpParam2": "the second parameter",
9999
},
100+
Headers: map[string]string{
101+
"X-Auth-Token": "the-first-parameter",
102+
"apiVersion": "v1",
103+
},
100104
},
101105
&HttpJson{
102106
client: mockHTTPClient{responseBody: response, statusCode: statusCode},
@@ -110,6 +114,10 @@ func genMockHttpJson(response string, statusCode int) []*HttpJson {
110114
"httpParam1": "12",
111115
"httpParam2": "the second parameter",
112116
},
117+
Headers: map[string]string{
118+
"X-Auth-Token": "the-first-parameter",
119+
"apiVersion": "v1",
120+
},
113121
TagKeys: []string{
114122
"role",
115123
"build",

0 commit comments

Comments
 (0)