Fix parsing of content-type field (#5356)

pull/5315/head
Matt Hook 3 years ago committed by GitHub
parent f603cd34be
commit cb3968b92f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

Loading…
Cancel
Save