From 2d1b2351eeaa9b18bf94c210951c6761b5cbf98a Mon Sep 17 00:00:00 2001 From: Neur0toxine Date: Wed, 18 Oct 2023 14:41:37 +0300 Subject: [PATCH] add json unmarshaling helper --- core/util/utils.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/core/util/utils.go b/core/util/utils.go index 92914c0..58280fa 100644 --- a/core/util/utils.go +++ b/core/util/utils.go @@ -1,11 +1,13 @@ package util import ( + "bytes" // nolint:gosec "crypto/sha1" "crypto/sha256" "encoding/json" "fmt" + "io" "net/http" "regexp" "strings" @@ -16,6 +18,7 @@ import ( "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/s3/s3manager" + "github.com/gin-gonic/gin" retailcrm "github.com/retailcrm/api-client-go/v2" v1 "github.com/retailcrm/mg-transport-api-client-go/v1" @@ -281,3 +284,15 @@ func GetCurrencySymbol(code string) string { func FormatCurrencyValue(value float32) string { return fmt.Sprintf("%.2f", value) } + +// BindJSONWithRaw will perform usual ShouldBindJSON and will return the original body data. +func BindJSONWithRaw(c *gin.Context, obj any) ([]byte, error) { + closer := c.Request.Body + defer func() { _ = closer.Close() }() + data, err := io.ReadAll(closer) + if err != nil { + return []byte, err + } + c.Request.Body = io.NopCloser(bytes.NewReader(data)) + return data, c.ShouldBindJSON(obj) +}