From 49dea4f598d050e1a1893596d23ecda39709d59e Mon Sep 17 00:00:00 2001 From: Ahmed Mohamed Date: Thu, 13 Apr 2023 17:15:10 +0200 Subject: [PATCH] body parser method (#9) --- context.go | 4 ++++ utils/json.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/context.go b/context.go index d678e91..d41503f 100644 --- a/context.go +++ b/context.go @@ -219,3 +219,7 @@ func (c *Context) JSON(code int, obj interface{}) ([]byte, error) { return []byte(jsonBody), nil } + +func (c *Context) BodyParser(v interface{}) error { + return utils.FromJSON(c.RequestCtx.Request.Body(), v) +} diff --git a/utils/json.go b/utils/json.go index 6dd1d9b..9179c70 100644 --- a/utils/json.go +++ b/utils/json.go @@ -9,3 +9,7 @@ func ToJSON(v interface{}) (string, error) { } return string(b), nil } + +func FromJSON(b []byte, v interface{}) error { + return json.Unmarshal(b, v) +}