Skip to content

Commit

Permalink
Additional request header parameters for the service
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Sosso committed Dec 30, 2015
1 parent bdac9b7 commit 54405ab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions plugins/httpjson/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
12 changes: 12 additions & 0 deletions plugins/httpjson/httpjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Service struct {
Method string
TagKeys []string
Parameters map[string]string
Headers map[string]string
}

type HTTPClient interface {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions plugins/httpjson/httpjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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",
Expand Down

0 comments on commit 54405ab

Please sign in to comment.