From 783a2dedfa4f4359abacd65f7d4f3f61aa8f339a Mon Sep 17 00:00:00 2001 From: Vladislav Veselskiy Date: Thu, 6 Feb 2025 12:17:09 +0300 Subject: [PATCH] Add func ReadPayloadFromRequest. --- push_common.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/push_common.go b/push_common.go index ecc4711..74e5797 100644 --- a/push_common.go +++ b/push_common.go @@ -2,6 +2,9 @@ package push_common import ( "context" + "encoding/json" + "io" + "net/http" "strconv" "strings" @@ -203,3 +206,15 @@ type PushPayload struct { ImageUrl string `json:"imageUrl"` TextVariants []*TextVariant `json:"textVariants"` } + +func ReadPayloadFromRequest(request *http.Request, payload *PushPayload) error { + b, err := io.ReadAll(request.Body) + if err != nil { + return err + } + if b == nil { + return errors.New("Can not read text variants. Request body bytes is nil.") + } + + return json.Unmarshal(b, payload) +}