cleaned up the httpjson POST function.

closes #688
closes #394
This commit is contained in:
Gabriel Levine 2016-02-12 21:37:51 -05:00 committed by Cameron Sparr
parent b14cfd6c64
commit 1837f83282
2 changed files with 20 additions and 6 deletions

View File

@ -34,6 +34,7 @@ and is in the `[agent]` config section.
- [#443](https://github.com/influxdata/telegraf/issues/443): Fix Ping command timeout parameter on Linux. - [#443](https://github.com/influxdata/telegraf/issues/443): Fix Ping command timeout parameter on Linux.
- [#662](https://github.com/influxdata/telegraf/pull/667): Change `[tags]` to `[global_tags]` to fix multiple-plugin tags bug. - [#662](https://github.com/influxdata/telegraf/pull/667): Change `[tags]` to `[global_tags]` to fix multiple-plugin tags bug.
- [#642](https://github.com/influxdata/telegraf/issues/642): Riemann output plugin issues. - [#642](https://github.com/influxdata/telegraf/issues/642): Riemann output plugin issues.
- [#394](https://github.com/influxdata/telegraf/issues/394): Support HTTP POST. Thanks @gabelev!
## v0.10.2 [2016-02-04] ## v0.10.2 [2016-02-04]

View File

@ -1,6 +1,7 @@
package httpjson package httpjson
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -58,7 +59,7 @@ var sampleConfig = `
"http://localhost:9998/stats/", "http://localhost:9998/stats/",
] ]
### HTTP method to use (case-sensitive) ### HTTP method to use: GET or POST (case-sensitive)
method = "GET" method = "GET"
### List of tag names to extract from top-level of JSON server response ### List of tag names to extract from top-level of JSON server response
@ -166,7 +167,8 @@ func (h *HttpJson) gatherServer(
return nil return nil
} }
// Sends an HTTP request to the server using the HttpJson object's HTTPClient // Sends an HTTP request to the server using the HttpJson object's HTTPClient.
// This request can be either a GET or a POST.
// Parameters: // Parameters:
// serverURL: endpoint to send request to // serverURL: endpoint to send request to
// //
@ -181,13 +183,24 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
} }
params := url.Values{} params := url.Values{}
for k, v := range h.Parameters { data := url.Values{}
params.Add(k, v)
switch {
case h.Method == "GET":
requestURL.RawQuery = params.Encode()
for k, v := range h.Parameters {
params.Add(k, v)
}
case h.Method == "POST":
requestURL.RawQuery = ""
for k, v := range h.Parameters {
data.Add(k, v)
}
} }
requestURL.RawQuery = params.Encode()
// Create + send request // Create + send request
req, err := http.NewRequest(h.Method, requestURL.String(), nil) req, err := http.NewRequest(h.Method, requestURL.String(), bytes.NewBufferString(data.Encode()))
if err != nil { if err != nil {
return "", -1, err return "", -1, err
} }