From cb3968b92ff47e6c295e330c5d2d62ce29cbc655 Mon Sep 17 00:00:00 2001 From: Matt Hook Date: Fri, 6 Aug 2021 16:39:26 +1200 Subject: [PATCH] Fix parsing of content-type field (#5356) --- api/http/proxy/factory/utils/json.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/api/http/proxy/factory/utils/json.go b/api/http/proxy/factory/utils/json.go index 2d44e17f4..23e7bc52f 100644 --- a/api/http/proxy/factory/utils/json.go +++ b/api/http/proxy/factory/utils/json.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "io/ioutil" + "mime" "gopkg.in/yaml.v3" ) @@ -69,7 +70,13 @@ func getBody(body io.ReadCloser, contentType string, isGzip bool) (interface{}, } func marshal(contentType string, data interface{}) ([]byte, error) { - switch contentType { + // Note: contentType can look like: "application/json" or "application/json; charset=utf-8" + mediaType, _, err := mime.ParseMediaType(contentType) + if err != nil { + return nil, err + } + + switch mediaType { case "application/yaml": return yaml.Marshal(data) case "application/json", "": @@ -80,7 +87,13 @@ func marshal(contentType string, data interface{}) ([]byte, error) { } func unmarshal(contentType string, body []byte, returnBody interface{}) error { - switch contentType { + // Note: contentType can look look like: "application/json" or "application/json; charset=utf-8" + mediaType, _, err := mime.ParseMediaType(contentType) + if err != nil { + return err + } + + switch mediaType { case "application/yaml": return yaml.Unmarshal(body, returnBody) case "application/json", "":