Skip to content

Commit

Permalink
cleaned up the httpjson POST function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Levine authored and geodimm committed Mar 10, 2016
1 parent 9821e3e commit b56a9f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- [#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.
- [#394](https://github.com/influxdata/telegraf/issues/394): Support HTTP POST. Thanks @gabelev!

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

Expand Down
25 changes: 19 additions & 6 deletions plugins/inputs/httpjson/httpjson.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httpjson

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -58,7 +59,7 @@ var sampleConfig = `
"http://localhost:9998/stats/",
]
### HTTP method to use (case-sensitive)
### HTTP method to use: GET or POST (case-sensitive)
method = "GET"
### List of tag names to extract from top-level of JSON server response
Expand Down Expand Up @@ -166,7 +167,8 @@ func (h *HttpJson) gatherServer(
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:
// serverURL: endpoint to send request to
//
Expand All @@ -181,13 +183,24 @@ func (h *HttpJson) sendRequest(serverURL string) (string, float64, error) {
}

params := url.Values{}
for k, v := range h.Parameters {
params.Add(k, v)
data := url.Values{}

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
req, err := http.NewRequest(h.Method, requestURL.String(), nil)
req, err := http.NewRequest(h.Method, requestURL.String(), bytes.NewBufferString(data.Encode()))
if err != nil {
return "", -1, err
}
Expand Down

0 comments on commit b56a9f4

Please sign in to comment.