From 5c5f137df7b028017308b5f9d7431bcfa65ab995 Mon Sep 17 00:00:00 2001 From: Emanuel Bennici Date: Mon, 27 Apr 2020 13:56:54 +0200 Subject: [PATCH] Use custom 'Content-Type' header if user has set it Let the user set `Content-Type` header. --- types/services/routine.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/types/services/routine.go b/types/services/routine.go index 871e7484..17f395b7 100644 --- a/types/services/routine.go +++ b/types/services/routine.go @@ -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))