Skip to content

Commit

Permalink
Merge pull request #15 from fnaoto/2022-06-16
Browse files Browse the repository at this point in the history
Change json decode to unmarshal.
  • Loading branch information
fnaoto authored Jun 16, 2022
2 parents 6637f84 + 2b31165 commit f35063d
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,23 @@ func (c *Client) NewRequest(httpRequest *HttpRequest) (*http.Response, error) {

func (c *Client) Decode(resp *http.Response, out interface{}) error {
defer resp.Body.Close()

buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)

if resp.StatusCode >= 300 {
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
return fmt.Errorf("status code is %v, body is %v", resp.StatusCode, buf.String())
}

if resp.ContentLength == 0 {
return nil
}
decoder := json.NewDecoder(resp.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(out)

err := json.Unmarshal(buf.Bytes(), &out)

if err != nil {
return fmt.Errorf("cloudn't decode json: %v to %T", resp.Body, out)
return fmt.Errorf("%v, output type is %T, body is %v", err, out, buf.String())
}

return nil
}

0 comments on commit f35063d

Please sign in to comment.