Skip to content

Commit

Permalink
Utilised json.Unmarshal instead of json.NewDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
moredure committed Apr 3, 2022
1 parent 45981da commit 1197ebd
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"crypto/tls"
"encoding/json"
"io"
"io/ioutil"
"net"
"net/http"
"strconv"
Expand Down Expand Up @@ -183,8 +183,14 @@ func (c *Client) PushWithContext(ctx Context, n *Notification) (*Response, error
response.StatusCode = httpRes.StatusCode
response.ApnsID = httpRes.Header.Get("apns-id")

decoder := json.NewDecoder(httpRes.Body)
if err := decoder.Decode(response); err != nil && err != io.EOF {
data, err := ioutil.ReadAll(httpRes.Body)
if err != nil {
return nil, err
}
if len(data) == 0 {
return response, nil
}
if err := json.Unmarshal(data, response); err != nil {
return &Response{}, err
}
return response, nil
Expand Down

0 comments on commit 1197ebd

Please sign in to comment.