Support custom success codes in http input (#6549)
This commit is contained in:
committed by
Daniel Nelson
parent
3802c8b8cb
commit
a01d273c45
@@ -29,6 +29,8 @@ type HTTP struct {
|
||||
Password string `toml:"password"`
|
||||
tls.ClientConfig
|
||||
|
||||
SuccessStatusCodes []int `toml:"success_status_codes"`
|
||||
|
||||
Timeout internal.Duration `toml:"timeout"`
|
||||
|
||||
client *http.Client
|
||||
@@ -71,6 +73,9 @@ var sampleConfig = `
|
||||
## Amount of time allowed to complete the HTTP request
|
||||
# timeout = "5s"
|
||||
|
||||
## List of success status codes
|
||||
# success_status_codes = [200]
|
||||
|
||||
## Data format to consume.
|
||||
## Each data format has its own unique set of configuration options, read
|
||||
## more about them here:
|
||||
@@ -101,6 +106,11 @@ func (h *HTTP) Init() error {
|
||||
},
|
||||
Timeout: h.Timeout.Duration,
|
||||
}
|
||||
|
||||
// Set default as [200]
|
||||
if len(h.SuccessStatusCodes) == 0 {
|
||||
h.SuccessStatusCodes = []int{200}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -171,12 +181,19 @@ func (h *HTTP) gatherURL(
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("Received status code %d (%s), expected %d (%s)",
|
||||
responseHasSuccessCode := false
|
||||
for _, statusCode := range h.SuccessStatusCodes {
|
||||
if resp.StatusCode == statusCode {
|
||||
responseHasSuccessCode = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !responseHasSuccessCode {
|
||||
return fmt.Errorf("received status code %d (%s), expected any value out of %v",
|
||||
resp.StatusCode,
|
||||
http.StatusText(resp.StatusCode),
|
||||
http.StatusOK,
|
||||
http.StatusText(http.StatusOK))
|
||||
h.SuccessStatusCodes)
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
|
||||
Reference in New Issue
Block a user