diff --git a/src/fetch.ts b/src/fetch.ts index c3881d9..0e8fc5b 100644 --- a/src/fetch.ts +++ b/src/fetch.ts @@ -77,7 +77,7 @@ export function createFetch ({ fetch }: CreateFetchOptions): $Fetch { request = withQuery(request, opts.params) } const hasPayload = payloadMethods.includes((opts.method || '').toLowerCase()) - if (opts.body && opts.body.toString() === '[object Object]' && hasPayload) { + if (opts.body && typeof opts.body === 'object' && hasPayload) { opts.body = JSON.stringify(opts.body) setHeader(opts, 'content-type', 'application/json') } diff --git a/test/index.test.mjs b/test/index.test.mjs index 99bdee3..165f78d 100644 --- a/test/index.test.mjs +++ b/test/index.test.mjs @@ -41,6 +41,9 @@ describe('ohmyfetch', () => { const { body } = await $fetch(getURL('post'), { method: 'POST', body: { num: 42 } }) expect(body).to.deep.eq({ num: 42 }) + const body2 = (await $fetch(getURL('post'), { method: 'POST', body: [{ num: 42 }, { num: 43 }] })).body + expect(body2).to.deep.eq([{ num: 42 }, { num: 43 }]) + const headerFetches = [ [['Content-Type', 'text/html']], [],