Skip to content

Commit

Permalink
swapped ioutil.ReadAll in favor of decoder.Decode
Browse files Browse the repository at this point in the history
  • Loading branch information
theriverman committed Sep 23, 2020
1 parent ebca0eb commit 9ad7543
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@ func newRawRequest(RequestType string, c *Client, ResponseBody interface{}, URL
defer resp.Body.Close()

// Evaluate response status code
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
decoder := json.NewDecoder(resp.Body)
if SuccessfulHTTPRequest(resp) {
if resp.StatusCode == http.StatusNoContent { // There's no body returned for 204 responses
return resp, nil
}
// We expect content so convert response JSON string to struct
json.Unmarshal([]byte(body), &ResponseBody) // responseBody contains a pointer to a struct
err = decoder.Decode(&ResponseBody)
if err != nil {
return nil, err
}
return resp, nil
}

return nil, fmt.Errorf("Request Failed. Returned body was:\n %s", body)
return nil, fmt.Errorf("Request Failed. Returned body was:\n %s", resp.Body)
}

0 comments on commit 9ad7543

Please sign in to comment.