From 1837f8328237dcd7573ca7bca498b5a2d7fc8201 Mon Sep 17 00:00:00 2001 From: Gabriel Levine Date: Fri, 12 Feb 2016 21:37:51 -0500 Subject: [PATCH] cleaned up the httpjson POST function. closes #688 closes #394 --- CHANGELOG.md | 1 + plugins/inputs/httpjson/httpjson.go | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d39cdf9c9..68dfb8dcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/plugins/inputs/httpjson/httpjson.go b/plugins/inputs/httpjson/httpjson.go index 4c3f4dee5..fa6ab70db 100644 --- a/plugins/inputs/httpjson/httpjson.go +++ b/plugins/inputs/httpjson/httpjson.go @@ -1,6 +1,7 @@ package httpjson import ( + "bytes" "errors" "fmt" "io/ioutil" @@ -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 @@ -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 // @@ -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 }