Merge pull request #537 from l0nax/bugfix/367_content-type_not-changeable

Use custom 'Content-Type' header if user has set it
pull/548/head
Hunter Long 2020-04-28 05:04:05 -07:00 committed by GitHub
commit dc1525c3d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 4 deletions

View File

@ -224,14 +224,24 @@ func CheckHttp(s *Service, record bool) *Service {
timeout := time.Duration(s.Timeout) * time.Second timeout := time.Duration(s.Timeout) * time.Second
var content []byte var content []byte
var res *http.Response var res *http.Response
var cnx string
var data *bytes.Buffer var data *bytes.Buffer
var headers []string var headers []string
contentType := "application/json" // default Content-Type
if s.Headers.Valid { if s.Headers.Valid {
headers = strings.Split(s.Headers.String, ",") headers = strings.Split(s.Headers.String, ",")
} else { } else {
headers = nil headers = nil
log.Warnf("Custom set Headers are not valid for Server '%s'!\n",
s.Name)
}
// check if 'Content-Type' header was defined
for _, header := range headers {
if strings.Split(header, "=")[0] == "Content-Type" {
contentType = strings.Split(header, "=")[1]
break
}
} }
if s.Redirect.Bool { if s.Redirect.Bool {
@ -244,11 +254,14 @@ func CheckHttp(s *Service, record bool) *Service {
data = bytes.NewBuffer(nil) data = bytes.NewBuffer(nil)
} }
if s.Method == "POST" { // force set Content-Type to 'application/json' if requests are made
cnx = "application/json" // with POST method
if s.Method == "POST" && contentType != "application/json" {
contentType = "application/json"
} }
content, res, err = utils.HttpRequest(s.Domain, s.Method, cnx, headers, data, timeout, s.VerifySSL.Bool) content, res, err = utils.HttpRequest(s.Domain, s.Method, contentType,
headers, data, timeout, s.VerifySSL.Bool)
if err != nil { if err != nil {
if record { if record {
recordFailure(s, fmt.Sprintf("HTTP Error %v", err)) recordFailure(s, fmt.Sprintf("HTTP Error %v", err))