Skip to content

Commit

Permalink
fix: use application/json for array body (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Narixius authored Nov 18, 2021
1 parent 4ab7022 commit e794b1e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down
3 changes: 3 additions & 0 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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']],
[],
Expand Down

0 comments on commit e794b1e

Please sign in to comment.