elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.79k stars 96 forks source link

1.7.8 introduce breaking behavior in payload json serialization #123

Closed c-eliasson closed 2 years ago

c-eliasson commented 2 years ago

With the release of 1.7.8, a breaking change was introduced in how payload serialization works when no explicit content-type has been set.

Under HTTP methods the documentation states:

NOTE: For methods having a body argument if the value is an Object it is assumed that it is a JSON payload and apply the same behaviour as calling .json(body), unless the Content-Type header has been set to something else beforehand.

We've relied on this and simply call .post({ foo: 'bar' }) to send a json serialized payload, without explicitly specifying the content-type. This has worked fine up until 1.7.7, but broke with the 1.7.8 release.

After digging through the changes in 1.7.8, I believe I've found the problematic commit. The problem seems to be this line. If I'm not missing something, it relies on no headers at all being set in order for the automatic json serialization to work. In our case we set the authorization header on all requests, which in our case means base._options.headers is defined, even though we haven't set any other headers.

I haven't had time to create a repro or look into a fix unfortunately, but after looking at the code I believe this is the issue.

Workaround

For anyone running into this issue, switching to 1.7.7 solve the problem.

elbywan commented 2 years ago

@c-eliasson Sorry for this, I'll investigate asap.

elbywan commented 2 years ago

@c-eliasson

Allright so I just released v1.7.9 including a fix which, I hope, will solve the problem.

Sorry again for the regression 🙇.

c-eliasson commented 2 years ago

@elbywan Wow, thanks for the swift reply! I'll take it for a spinn as soon as I get a chance. Will get back to you shortly.

c-eliasson commented 2 years ago

@elbywan Can confirm that bumping to 1.7.9 resolved the issue, and the automatic payload serialization now appear to work as expected for us again.

Thanks for the quick fix, and the great work you're doing with wretch! 👏