1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-23 15:29:42 +02:00

Fix parsing of content-type field (#5356)

This commit is contained in:
Matt Hook 2021-08-06 16:39:26 +12:00 committed by GitHub
parent f603cd34be
commit cb3968b92f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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", "":