Skip to content

Commit

Permalink
take a request body as a param
Browse files Browse the repository at this point in the history
  • Loading branch information
lswith authored and sparrc committed Apr 7, 2016
1 parent f947fa8 commit 73a7916
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 5 additions & 1 deletion plugins/inputs/http_response/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ This input plugin will test HTTP/HTTPS connections.
address = "http://github.com"
## Set response_timeout (default 10 seconds)
response_timeout = 10
## HTTP Method
## HTTP Request Method
method = "GET"
## HTTP Request Headers
headers = '''
Host: github.com
'''
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
## Optional HTTP Request Body
body = '''
{'fake':'data'}
'''
```

### Measurements & Fields:
Expand Down
20 changes: 13 additions & 7 deletions plugins/inputs/http_response/http_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package http_response
import (
"bufio"
"errors"
"fmt"
"io"
"net/http"
"net/textproto"
"net/url"
"os"
"strings"
"time"

Expand All @@ -18,6 +17,7 @@ import (
// HTTPResponse struct
type HTTPResponse struct {
Address string
Body string
Method string
ResponseTimeout int
Headers string
Expand All @@ -34,21 +34,26 @@ var sampleConfig = `
address = "http://github.com"
## Set response_timeout (default 10 seconds)
response_timeout = 10
## HTTP Method
## HTTP Request Method
method = "GET"
## HTTP Request Headers
headers = '''
Host: github.com
'''
## Whether to follow redirects from the server (defaults to false)
follow_redirects = true
## Optional HTTP Request Body
body = '''
{'fake':'data'}
'''
`

// SampleConfig returns the plugin SampleConfig
func (h *HTTPResponse) SampleConfig() string {
return sampleConfig
}

// ErrRedirectAttempted indicates that a redirect occurred
var ErrRedirectAttempted = errors.New("redirect")

// HTTPGather gathers all fields and returns any errors it encounters
Expand All @@ -61,13 +66,16 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
}

if h.FollowRedirects == false {
fmt.Println(h.FollowRedirects)
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
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 {
return nil, err
}
Expand All @@ -81,15 +89,13 @@ func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
request.Header = http.Header(mimeHeader)
// Start Timer
start := time.Now()
request.Write(os.Stdout)
resp, err := client.Do(request)
if err != nil {
if h.FollowRedirects {
return nil, err
}
if urlError, ok := err.(*url.Error); ok &&
urlError.Err == ErrRedirectAttempted {
fmt.Println(err)
err = nil
} else {
return nil, err
Expand Down

0 comments on commit 73a7916

Please sign in to comment.