go-resty / resty

Simple HTTP and REST client library for Go
MIT License
10.08k stars 710 forks source link

Non nil body but empty in response cause 'unexpected end of JSON input' error #678

Closed renathoaz closed 1 year ago

renathoaz commented 1 year ago

resty v2.7.0 go 1.20

@jeevatkm I was implementing one client and the server is probably with issue in one of the routes that sends 201 and body is empty but r.body is getting initialized somehow although its length is 0. Maybe empty string is coming from server. It also affects SetResult(), maybe a check in parseResponseBody is also needed to prevent error from unmarshalling empty []byte{}

if we change from:

func (r *Response) fmtBodyString(sl int64) string {
    if r.body != nil  {
        if int64(len(r.body)) > sl {
...

Actual: Here we can see there's nothing in body, maybe empty string? but Content-Type is application/json; charset=utf-8

Seleção_034

to:

func (r *Response) fmtBodyString(sl int64) string {
    if r.body != nil && len(r.body) > 0 {
        if int64(len(r.body)) > sl {
...

After this small change everything is fine now:

Seleção_033

jeevatkm commented 1 year ago

@renathoaz Thanks for bringing it up. I noticed your suggestion in PRs #654 and #674; once merged, it will resolve.