Skip to content

Commit b268a30

Browse files
committed
Fix missing content-type header with JSON bodies
Change-type: patch
1 parent 11adf38 commit b268a30

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ export default class PineFetch<
2929
url: string;
3030
body?: AnyObject;
3131
} & AnyObject) {
32-
const normalizedBody =
33-
body == null
34-
? null
35-
: typeof body === 'object'
36-
? JSON.stringify(body)
37-
: body;
32+
let normalizedBody: string | undefined;
33+
if (body != null && typeof body === 'object') {
34+
// If body is an object then JSON.stringify it and set the matching content-type header
35+
options.headers ??= {};
36+
options.headers['Content-Type'] = 'application/json';
37+
normalizedBody = JSON.stringify(body);
38+
} else {
39+
normalizedBody = body;
40+
}
3841

3942
const response = await fetch(url, {
4043
...options,

0 commit comments

Comments
 (0)