Skip to content

Commit 8ecad47

Browse files
CalebM1987Rendez
authored andcommitted
fix(fetcher): do not send json content type with empty body
1 parent f9256aa commit 8ecad47

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/fetcher.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ function getFetchParams(request: Request): {
131131
const path = getPath(request.path, payload)
132132
const query = getQuery(request.method, payload, request.queryParams)
133133
const body = getBody(request.method, payload)
134-
const headers = getHeaders(body, request.init?.headers)
134+
const headers = sendBody(request.method)
135+
? getHeaders(body, request.init?.headers)
136+
: new Headers(request.init?.headers)
135137
const url = request.baseUrl + path + query
136138

137139
const init = {

test/fetch.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ describe('fetch', () => {
2424

2525
const expectedHeaders = {
2626
authorization: 'Bearer token',
27-
accept: 'application/json',
2827
}
2928

30-
const headersWithContentType = {
29+
const expectedHeadersWithBody = {
3130
...expectedHeaders,
3231
'content-type': 'application/json',
32+
accept: 'application/json',
3333
}
3434

3535
it('GET /query/{a}/{b}', async () => {
@@ -64,7 +64,7 @@ describe('fetch', () => {
6464
expect(data.params).toEqual({ id: '1' })
6565
expect(data.body).toEqual({ list: ['b', 'c'] })
6666
expect(data.query).toEqual({})
67-
expect(data.headers).toEqual(headersWithContentType)
67+
expect(data.headers).toEqual(expectedHeadersWithBody)
6868
})
6969
})
7070

@@ -77,7 +77,7 @@ describe('fetch', () => {
7777
expect(data.params).toEqual({ id: '1' })
7878
expect(data.body).toEqual(['b', 'c'])
7979
expect(data.query).toEqual({})
80-
expect(data.headers).toEqual(headersWithContentType)
80+
expect(data.headers).toEqual(expectedHeadersWithBody)
8181
})
8282
})
8383

@@ -97,7 +97,7 @@ describe('fetch', () => {
9797
expect(data.params).toEqual({ id: '1' })
9898
expect(data.body).toEqual({ list: ['b', 'c'] })
9999
expect(data.query).toEqual({ scalar: 'a' })
100-
expect(data.headers).toEqual(headersWithContentType)
100+
expect(data.headers).toEqual(expectedHeadersWithBody)
101101
})
102102
})
103103

0 commit comments

Comments
 (0)