Skip to content

Commit

Permalink
Improve 204 no content handling (#654)
Browse files Browse the repository at this point in the history
* set request error to nil upon 204 No Content response

* fix empty body check (ioutil.ReadAll will return an empty slice rather than nil https://go.googlesource.com/go/+/refs/tags/go1.16/src/io/io.go#627)
  • Loading branch information
haivp3010 committed Sep 17, 2023
1 parent e12ab69 commit 6ea5279
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ func responseLogger(c *Client, res *Response) error {

func parseResponseBody(c *Client, res *Response) (err error) {
if res.StatusCode() == http.StatusNoContent {
res.Request.Error = nil
return
}
// Handles only JSON or XML content type
Expand Down
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *Response) Cookies() []*http.Cookie {

// String method returns the body of the server response as String.
func (r *Response) String() string {
if r.body == nil {
if len(r.body) == 0 {
return ""
}
return strings.TrimSpace(string(r.body))
Expand Down Expand Up @@ -156,7 +156,7 @@ func (r *Response) setReceivedAt() {
}

func (r *Response) fmtBodyString(sl int64) string {
if r.body != nil {
if len(r.body) > 0 {
if int64(len(r.body)) > sl {
return fmt.Sprintf("***** RESPONSE TOO LARGE (size - %d) *****", len(r.body))
}
Expand Down

0 comments on commit 6ea5279

Please sign in to comment.