cleaned up the httpjson POST function.
This commit is contained in:
parent
512d8bc5a0
commit
2f88930c33
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue