Skip to content

Commit

Permalink
add json and xml body parser
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 23, 2022
1 parent 58fb53b commit af78131
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,27 @@ type Config struct {
// Default: json.Marshal
JSONEncoder utils.JSONMarshal `json:"-"`

// When set by an external client of Fiber it will use the provided implementation of a
// JSONUnmarshal
//
// Allowing for flexibility in using another json library for decoding
// Default: json.Unmarshal
JSONDecoder utils.JSONUnmarshal `json:"-"`

// XMLEncoder set by an external client of Fiber it will use the provided implementation of a
// XMLMarshal
//
// Allowing for flexibility in using another XML library for encoding
// Default: xml.Marshal
XMLEncoder utils.XMLMarshal `json:"-"`

// XMLDecoder set by an external client of Fiber it will use the provided implementation of a
// XMLUnmarshal
//
// Allowing for flexibility in using another XML library for encoding
// Default: utils.XMLUnmarshal
XMLDecoder utils.XMLUnmarshal `json:"-"`

// App validate. if nil, and context.Validate will always return a error.
// Default: nil
Validator Validator
Expand Down Expand Up @@ -510,10 +524,17 @@ func New(config ...Config) *App {
if app.config.JSONEncoder == nil {
app.config.JSONEncoder = json.Marshal
}
if app.config.JSONDecoder == nil {
app.config.JSONDecoder = json.Unmarshal
}

if app.config.XMLEncoder == nil {
app.config.XMLEncoder = xml.Marshal
}
if app.config.XMLDecoder == nil {
app.config.XMLDecoder = xml.Unmarshal
}

if app.config.Network == "" {
app.config.Network = NetworkTCP4
}
Expand Down
16 changes: 16 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,22 @@ func (c *DefaultCtx) Body() []byte {
return body
}

func (c *DefaultCtx) BodyJSON(v any) error {
if !bytes.HasPrefix(c.Request().Header.ContentType(), utils.UnsafeBytes(MIMEApplicationJSON)) {
return NewError(http.StatusUnsupportedMediaType, "expecting content-type \"application/json\"")
}

return c.app.config.JSONDecoder(c.Body(), v)
}

func (c *DefaultCtx) BodyXML(v any) error {
if !bytes.HasPrefix(c.Request().Header.ContentType(), utils.UnsafeBytes(MIMEApplicationXML)) {
return NewError(http.StatusUnsupportedMediaType, "expecting content-type \"application/xml\"")
}

return c.app.config.XMLDecoder(c.Body(), v)
}

// ClearCookie expires a specific cookie by key on the client side.
// If no key is provided it expires all cookies that came with the request.
func (c *DefaultCtx) ClearCookie(key ...string) {
Expand Down
6 changes: 6 additions & 0 deletions ctx_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ type Ctx interface {
// Make copies or use the Immutable setting instead.
Body() []byte

// BodyJSON will check the content-type and unmarshal request body with Config.JSONDecoder
BodyJSON(v any) error

// BodyXML will check the content-type and unmarshal request body with Config.XMLDecoder
BodyXML(v any) error

// ClearCookie expires a specific cookie by key on the client side.
// If no key is provided it expires all cookies that came with the request.
ClearCookie(key ...string)
Expand Down

0 comments on commit af78131

Please sign in to comment.