From 06a298ef7606458b50272d132a66bb8cfd0aa78f Mon Sep 17 00:00:00 2001 From: Roopak Venkatakrishnan Date: Tue, 24 Sep 2019 07:51:55 -0700 Subject: [PATCH] Reset after reading (#118) --- webhooks.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/webhooks.go b/webhooks.go index f98ee06..7cd7625 100644 --- a/webhooks.go +++ b/webhooks.go @@ -1,6 +1,7 @@ package paypal import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -19,15 +20,14 @@ func (c *Client) VerifyWebhookSignature(httpReq *http.Request, webhookID string) WebhookID string `json:"webhook_id,omitempty"` WebhookEvent json.RawMessage `json:"webhook_event"` } - getBody := httpReq.GetBody - bodyReadCloser, err := getBody() - if err != nil { - return nil, err - } - body, err := ioutil.ReadAll(bodyReadCloser) - if err != nil { - return nil, err + + // Read the content + var bodyBytes []byte + if httpReq.Body != nil { + bodyBytes, _ = ioutil.ReadAll(httpReq.Body) } + // Restore the io.ReadCloser to its original state + httpReq.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) verifyRequest := verifyWebhookSignatureRequest{ AuthAlgo: httpReq.Header.Get("PAYPAL-AUTH-ALGO"), @@ -36,7 +36,7 @@ func (c *Client) VerifyWebhookSignature(httpReq *http.Request, webhookID string) TransmissionSig: httpReq.Header.Get("PAYPAL-TRANSMISSION-SIG"), TransmissionTime: httpReq.Header.Get("PAYPAL-TRANSMISSION-TIME"), WebhookID: webhookID, - WebhookEvent: json.RawMessage(body), + WebhookEvent: json.RawMessage(bodyBytes), } response := &VerifyWebhookResponse{}