Fetch not calling JSON.stringify for body objects.
Hey, i'm currently using
@sapphire/fetch
and trying to send a POST request for a API with a object on the body. The thing is: the object is not being stringified and the API is receiving [object Object]
.
I went through the @sapphire/fetch
source code and, apparently, the function that determines if a body should be stringified or not (shouldJsonStringify
) is returning false
for my object. (https://github.com/sapphiredev/utilities/blob/main/packages/fetch/src/lib/fetch.ts#L191-L208)
And just to make it clear, the object that i'm trying to send is in fact...a object, because using typeof theObject
returns object
lol, but its doesn't have a toJSON
function or a Object constructor, in fact it has a [Function: Object]
constructor, which is not included in the shouldJsonStringify
function.
Hope that this is enough information, happy to create a issue or maybe create a pull request with a fix. Downgrading the lib to 3.0.2
is my workaround for now.GitHub
utilities/packages/fetch/src/lib/fetch.ts at main · sapphiredev/uti...
Common JavaScript utilities for Sapphire Projects. Contribute to sapphiredev/utilities development by creating an account on GitHub.
7 Replies
What's your object?
We made that change because I use the lib at work as well and we needed to be able to upload Blobs and we have this running in DTA for some weeks now without problems. That includes regular objects.
it's pretty much this
{ email: "[email protected]", password: "abc123" }
. it comes from a zod z.infer
of a schema
can provide a code snippet too, i am just not on my computer rn
I think this just another case of JavaScript is being weird, and having some plain objects with different constructors than Object
for some reasonplease provide a repro sample yes
My best guess is that this is some weird quirk of zod so we'll need a repro to ensure we can support it
Yep, they probably do Object.create so I’ll have to check
made a repro sample, it's pretty much like this:
lmk if u guys need more info, happy to provide
I can't repro
hmmm wonder if it's being caused by something else then
i will take a look later today