Skip to content

Commit

Permalink
🚧 refactor bodyparser
Browse files Browse the repository at this point in the history
Co-Authored-By: Mones Zarrugh <11161902+moneszarrugh@users.noreply.github.com>

#1067
  • Loading branch information
Fenny committed Dec 10, 2020
1 parent 2d5ed06 commit a1879ce
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ var decoderPool = &sync.Pool{New: func() interface{} {
// BodyParser binds the request body to a struct.
// It supports decoding the following content types based on the Content-Type header:
// application/json, application/xml, application/x-www-form-urlencoded, multipart/form-data
// If none of the content types above are matched, it will return a ErrUnprocessableEntity error
func (c *Ctx) BodyParser(out interface{}) error {
// Get decoder from pool
schemaDecoder := decoderPool.Get().(*schema.Decoder)
Expand All @@ -259,26 +260,29 @@ func (c *Ctx) BodyParser(out interface{}) error {
if strings.HasPrefix(ctype, MIMEApplicationJSON) {
schemaDecoder.SetAliasTag("json")
return json.Unmarshal(c.fasthttp.Request.Body(), out)
} else if strings.HasPrefix(ctype, MIMEApplicationForm) {
}
if strings.HasPrefix(ctype, MIMEApplicationForm) {
schemaDecoder.SetAliasTag("form")
data := make(map[string][]string)
c.fasthttp.PostArgs().VisitAll(func(key []byte, val []byte) {
data[getString(key)] = append(data[getString(key)], getString(val))
})
return schemaDecoder.Decode(out, data)
} else if strings.HasPrefix(ctype, MIMEMultipartForm) {
}
if strings.HasPrefix(ctype, MIMEMultipartForm) {
schemaDecoder.SetAliasTag("form")
data, err := c.fasthttp.MultipartForm()
if err != nil {
return err
}
return schemaDecoder.Decode(out, data.Value)
} else if strings.HasPrefix(ctype, MIMETextXML) || strings.HasPrefix(ctype, MIMEApplicationXML) {
}
if strings.HasPrefix(ctype, MIMETextXML) || strings.HasPrefix(ctype, MIMEApplicationXML) {
schemaDecoder.SetAliasTag("xml")
return xml.Unmarshal(c.fasthttp.Request.Body(), out)
}
// No suitable content type found
return fmt.Errorf("bodyparser: cannot parse content-type: %v", ctype)
return ErrUnprocessableEntity
}

// ClearCookie expires a specific cookie by key on the client side.
Expand Down

0 comments on commit a1879ce

Please sign in to comment.