Use custom 'Content-Type' header if user has set it

Let the user set `Content-Type` header.
pull/537/head
Emanuel Bennici 2020-04-27 13:56:54 +02:00
parent e4ed216833
commit 5c5f137df7
No known key found for this signature in database
GPG Key ID: 17FA2D56BAD01661
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
var content []byte
var res *http.Response
var cnx string
var data *bytes.Buffer
var headers []string
contentType := "application/json" // default Content-Type
if s.Headers.Valid {
headers = strings.Split(s.Headers.String, ",")
} else {
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 {
@ -244,11 +254,14 @@ func CheckHttp(s *Service, record bool) *Service {
data = bytes.NewBuffer(nil)
}
if s.Method == "POST" {
cnx = "application/json"
// force set Content-Type to 'application/json' if requests are made
// 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 record {
recordFailure(s, fmt.Sprintf("HTTP Error %v", err))