Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
renathoaz opened this issue Jul 20, 2023 · 1 comment · Fixed by #654
Closed

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

renathoaz opened this issue Jul 20, 2023 · 1 comment · Fixed by #654
Labels

Comments

@renathoaz
Copy link

renathoaz commented Jul 20, 2023

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging a pull request may close this issue.

2 participants