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
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))