elbywan / wretch

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

Request body not stringified for application/vnd.api+json #122

Closed bmaclean closed 2 years ago

bmaclean commented 2 years ago

Related Issues

Related #90

Bug / Improvement Overview

The body of requests sent with Content-Type: 'application/vnd.api+json' aren't being stringified, resulting in an unserialized [object Object] payload. This Content-Type is used for (de)serialization in many Rails API's.

Could this Content-Type be considered as json for the method base here:

https://github.com/elbywan/wretch/blob/master/src/wretcher.ts#L202

We're currently either having to call .[HTTP method](JSON.stringify(body)) to process our payloads using Content-Type: 'application/vnd.api+json', as json() overwrites the Content-Type. Could the verification for whether the request should be serialized as JSON be expanded beyond checking if the Content-Type begins is a application/json substring?

bmaclean commented 2 years ago

Just to note, I ended up patching this using middleware to serialize outgoing Object payloads. Still wondering if this could be included in the Wretch spec, though. Thanks!

elbywan commented 2 years ago

Hey @bmaclean,

Thanks for reporting this issue!

Just to note, I ended up patching this using middleware to serialize outgoing Object payloads. Still wondering if this could be included in the Wretch spec, though. Thanks!

Glad to know that you found a workaround!

Could the verification for whether the request should be serialized as JSON be expanded beyond checking if the Content-Type begins is a application/json substring?

Yes, I just pushed a commit to relax the check, the content-type is now considered targeting json if it passes the following check:/^application\/.*json.*/.

I think that it should do the trick!