take a request body as a param
This commit is contained in:
parent
f947fa86e3
commit
73a7916ce3
|
@ -11,7 +11,7 @@ This input plugin will test HTTP/HTTPS connections.
|
||||||
address = "http://github.com"
|
address = "http://github.com"
|
||||||
## Set response_timeout (default 10 seconds)
|
## Set response_timeout (default 10 seconds)
|
||||||
response_timeout = 10
|
response_timeout = 10
|
||||||
## HTTP Method
|
## HTTP Request Method
|
||||||
method = "GET"
|
method = "GET"
|
||||||
## HTTP Request Headers
|
## HTTP Request Headers
|
||||||
headers = '''
|
headers = '''
|
||||||
|
@ -19,6 +19,10 @@ This input plugin will test HTTP/HTTPS connections.
|
||||||
'''
|
'''
|
||||||
## Whether to follow redirects from the server (defaults to false)
|
## Whether to follow redirects from the server (defaults to false)
|
||||||
follow_redirects = true
|
follow_redirects = true
|
||||||
|
## Optional HTTP Request Body
|
||||||
|
body = '''
|
||||||
|
{'fake':'data'}
|
||||||
|
'''
|
||||||
```
|
```
|
||||||
|
|
||||||
### Measurements & Fields:
|
### Measurements & Fields:
|
||||||
|
|
|
@ -3,11 +3,10 @@ package http_response
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -18,6 +17,7 @@ import (
|
||||||
// HTTPResponse struct
|
// HTTPResponse struct
|
||||||
type HTTPResponse struct {
|
type HTTPResponse struct {
|
||||||
Address string
|
Address string
|
||||||
|
Body string
|
||||||
Method string
|
Method string
|
||||||
ResponseTimeout int
|
ResponseTimeout int
|
||||||
Headers string
|
Headers string
|
||||||
|
@ -34,7 +34,7 @@ var sampleConfig = `
|
||||||
address = "http://github.com"
|
address = "http://github.com"
|
||||||
## Set response_timeout (default 10 seconds)
|
## Set response_timeout (default 10 seconds)
|
||||||
response_timeout = 10
|
response_timeout = 10
|
||||||
## HTTP Method
|
## HTTP Request Method
|
||||||
method = "GET"
|
method = "GET"
|
||||||
## HTTP Request Headers
|
## HTTP Request Headers
|
||||||
headers = '''
|
headers = '''
|
||||||
|
@ -42,6 +42,10 @@ var sampleConfig = `
|
||||||
'''
|
'''
|
||||||
## Whether to follow redirects from the server (defaults to false)
|
## Whether to follow redirects from the server (defaults to false)
|
||||||
follow_redirects = true
|
follow_redirects = true
|
||||||
|
## Optional HTTP Request Body
|
||||||
|
body = '''
|
||||||
|
{'fake':'data'}
|
||||||
|
'''
|
||||||
`
|
`
|
||||||
|
|
||||||
// SampleConfig returns the plugin SampleConfig
|
// SampleConfig returns the plugin SampleConfig
|
||||||
|
@ -49,6 +53,7 @@ func (h *HTTPResponse) SampleConfig() string {
|
||||||
return sampleConfig
|
return sampleConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ErrRedirectAttempted indicates that a redirect occurred
|
||||||
var ErrRedirectAttempted = errors.New("redirect")
|
var ErrRedirectAttempted = errors.New("redirect")
|
||||||
|
|
||||||
// HTTPGather gathers all fields and returns any errors it encounters
|
// HTTPGather gathers all fields and returns any errors it encounters
|
||||||
|
@ -61,13 +66,16 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if h.FollowRedirects == false {
|
if h.FollowRedirects == false {
|
||||||
fmt.Println(h.FollowRedirects)
|
|
||||||
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
|
||||||
return ErrRedirectAttempted
|
return ErrRedirectAttempted
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
request, err := http.NewRequest(h.Method, h.Address, nil)
|
var body io.Reader
|
||||||
|
if h.Body != "" {
|
||||||
|
body = strings.NewReader(h.Body)
|
||||||
|
}
|
||||||
|
request, err := http.NewRequest(h.Method, h.Address, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -81,7 +89,6 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
|
||||||
request.Header = http.Header(mimeHeader)
|
request.Header = http.Header(mimeHeader)
|
||||||
// Start Timer
|
// Start Timer
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
request.Write(os.Stdout)
|
|
||||||
resp, err := client.Do(request)
|
resp, err := client.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if h.FollowRedirects {
|
if h.FollowRedirects {
|
||||||
|
@ -89,7 +96,6 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
|
||||||
}
|
}
|
||||||
if urlError, ok := err.(*url.Error); ok &&
|
if urlError, ok := err.(*url.Error); ok &&
|
||||||
urlError.Err == ErrRedirectAttempted {
|
urlError.Err == ErrRedirectAttempted {
|
||||||
fmt.Println(err)
|
|
||||||
err = nil
|
err = nil
|
||||||
} else {
|
} else {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
Loading…
Reference in New Issue