Skip to content

Commit 1837f83

Browse files
Gabriel Levinesparrc
Gabriel Levine
authored andcommitted
cleaned up the httpjson POST function.
closes influxdata#688 closes influxdata#394
1 parent b14cfd6 commit 1837f83

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and is in the `[agent]` config section.
3434
- [#443](https://github.com/influxdata/telegraf/issues/443): Fix Ping command timeout parameter on Linux.
3535
- [#662](https://github.com/influxdata/telegraf/pull/667): Change `[tags]` to `[global_tags]` to fix multiple-plugin tags bug.
3636
- [#642](https://github.com/influxdata/telegraf/issues/642): Riemann output plugin issues.
37+
- [#394](https://github.com/influxdata/telegraf/issues/394): Support HTTP POST. Thanks @gabelev!
3738

3839
## v0.10.2 [2016-02-04]
3940

plugins/inputs/httpjson/httpjson.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package httpjson
22

33
import (
4+
"bytes"
45
"errors"
56
"fmt"
67
"io/ioutil"
@@ -58,7 +59,7 @@ var sampleConfig = `
5859
"http://localhost:9998/stats/",
5960
]
6061
61-
### HTTP method to use (case-sensitive)
62+
### HTTP method to use: GET or POST (case-sensitive)
6263
method = "GET"
6364
6465
### List of tag names to extract from top-level of JSON server response
@@ -166,7 +167,8 @@ func (h *HttpJson) gatherServer(
166167
return nil
167168
}
168169

169-
// Sends an HTTP request to the server using the HttpJson object's HTTPClient
170+
// Sends an HTTP request to the server using the HttpJson object's HTTPClient.
171+
// This request can be either a GET or a POST.
170172
// Parameters:
171173
// serverURL: endpoint to send request to
172174
//
@@ -181,13 +183,24 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
181183
}
182184

183185
params := url.Values{}
184-
for k, v := range h.Parameters {
185-
params.Add(k, v)
186+
data := url.Values{}
187+
188+
switch {
189+
case h.Method == "GET":
190+
requestURL.RawQuery = params.Encode()
191+
for k, v := range h.Parameters {
192+
params.Add(k, v)
193+
}
194+
195+
case h.Method == "POST":
196+
requestURL.RawQuery = ""
197+
for k, v := range h.Parameters {
198+
data.Add(k, v)
199+
}
186200
}
187-
requestURL.RawQuery = params.Encode()
188201

189202
// Create + send request
190-
req, err := http.NewRequest(h.Method, requestURL.String(), nil)
203+
req, err := http.NewRequest(h.Method, requestURL.String(), bytes.NewBufferString(data.Encode()))
191204
if err != nil {
192205
return "", -1, err
193206
}

0 commit comments

Comments
 (0)