Skip to content

Commit

Permalink
Merge pull request #3 from cloudflare/fix-content-type
Browse files Browse the repository at this point in the history
Fixed Content-Type header handling
  • Loading branch information
David Kitchen committed Nov 25, 2015
2 parents bfa0229 + a266ef9 commit be5f6ff
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions decoder/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"mime"
)

var (
Expand All @@ -23,7 +23,11 @@ var (
// content into the supplied interface. If the content-type of the request is
// not one that matches a known decoder, then an error will be thrown
func Decode(req *http.Request, v interface{}) error {
contentType := getContentType(req)
contentType, err := getContentType(req)
if err != nil {
return err
}

switch contentType {
case "application/json":
return jsonDecode(req, v)
Expand All @@ -34,9 +38,8 @@ func Decode(req *http.Request, v interface{}) error {
}
}

func getContentType(req *http.Request) (contentType string) {
contentType = strings.TrimSpace(req.Header.Get("Content-Type"))

func getContentType(req *http.Request) (contentType string, err error) {
contentType, _, err = mime.ParseMediaType(req.Header.Get("Content-Type"))
return
}

Expand Down

0 comments on commit be5f6ff

Please sign in to comment.