Skip to content

Commit

Permalink
fix: add accept header when using json payload (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Atinux committed Nov 22, 2021
1 parent 375b825 commit 662145f
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ await $fetch('/movie?lang=en', { parseResponse: txt => txt })

## ✔️ JSON Body

`$fetch` automatically stringifies request body (if an object is passed) and adds JSON `Content-Type` headers (for `put`, `patch` and `post` requests).
`$fetch` automatically stringifies request body (if an object is passed) and adds JSON `Content-Type` and `Accept` headers (for `put`, `patch` and `post` requests).

```js
const { users } = await $fetch('/api/users', { method: 'POST', body: { some: 'json' } })
Expand Down
1 change: 1 addition & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function createFetch ({ fetch }: CreateFetchOptions): $Fetch {
if (opts.body && typeof opts.body === 'object' && hasPayload) {
opts.body = JSON.stringify(opts.body)
setHeader(opts, 'content-type', 'application/json')
setHeader(opts, 'accept', 'application/json')
}
}
const response: FetchResponse<any> = await fetch(request, opts as RequestInit).catch(error => onError(request, opts, error, undefined))
Expand Down
1 change: 1 addition & 0 deletions test/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('ohmyfetch', () => {
for (const sentHeaders of headerFetches) {
const { headers } = await $fetch(getURL('post'), { method: 'POST', body: { num: 42 }, headers: sentHeaders })
expect(headers).to.include({ 'content-type': 'application/json' })
expect(headers).to.include({ accept: 'application/json' })
}
})

Expand Down

0 comments on commit 662145f

Please sign in to comment.