Skip to content

Commit 2ca7c6c

Browse files
committed
fix: 🐛 nodejs content-length calculation
1 parent 7af1de7 commit 2ca7c6c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

packages/core/src/http-adapter/http-adapter.server.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ export const getAdapter = () =>
6666
onBeforeRequest();
6767

6868
if (payload) {
69-
options.headers["Content-Length"] = Buffer.byteLength(JSON.stringify(payload));
69+
// Calculate Content-Length based on the exact bytes that will be sent.
70+
// If payload is already a string or Buffer (as produced by getAdapterPayload),
71+
// avoid double-stringifying which would lead to incorrect Content-Length.
72+
const payloadAsAny = payload as unknown;
73+
const isString = typeof payloadAsAny === "string";
74+
const isBuffer = Buffer.isBuffer(payloadAsAny);
75+
const contentLength =
76+
isString || isBuffer
77+
? Buffer.byteLength(payloadAsAny as string | Buffer)
78+
: Buffer.byteLength(JSON.stringify(payloadAsAny));
79+
options.headers["Content-Length"] = contentLength;
7080
}
7181

7282
const queryString = queryParams ? stringifyQueryParams(queryParams) : "";

0 commit comments

Comments
 (0)