Merge pull request #41 from altperfect/fix/enforce-single-content-type

Fix EnforceSingleContentType function to not remove all custom headers
This commit is contained in:
Tony West authored and GitHub committed 2025-03-18 16:06:26 -04:00
commit 83d1b57aef
1 file changed
+10 -16
+10 -16
View File
@@ -679,22 +679,16 @@ func ExtractSpecFromJS(bodyBytes []byte) []byte {
func EnforceSingleContentType(newContentType string) {
newContentType = strings.TrimSpace(newContentType)
if Headers != nil {
headerString := strings.Join(Headers, ",")
Headers = nil
ctIndex := strings.Index(strings.ToLower(headerString), "content-type:") + 14
headerString = headerString[ctIndex:]
if strings.Contains(headerString, ",") {
headerString = strings.TrimPrefix(headerString, ",")
ctEndIndex := strings.Index(headerString[ctIndex:], ",") + 1
headerString = headerString[:ctEndIndex]
} else if !strings.Contains(headerString, ":") {
headerString = ""
}
if headerString != "" {
Headers = append(Headers, strings.Split(headerString, ",")...)
}
}
// Remove old 'Content-Type' header
slices.DeleteFunc(Headers, func(h string) bool {
return strings.HasPrefix(strings.ToLower(h), "content-type:")
})
Headers = append(Headers, "Content-Type: "+newContentType)
// Remove empty elements to avoid repetitions of "-H ''"
Headers = slices.DeleteFunc(Headers, func(h string) bool {
return strings.TrimSpace(h) == ""
})
}