From 550b4f03065935c161011f67af6470ae74e9fd39 Mon Sep 17 00:00:00 2001 From: Christoffer Date: Tue, 3 Jun 2025 15:52:54 +0200 Subject: [PATCH] Calculate correct content length for UTF-8 string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By using `Content-Length: request.length`, you risk telling the server the wrong byte‐count. Consider this payload: ``` const response = await client.registerAccount({ Firstname: "Christöffer", Lastname: "Skeppstedt", ... }); ``` When the string contains an "ö" instead of "o", we have: - Byte count: 930 - String length: 929 When this request is sent to the Trustly API, it responds back with: ``` { "name": "JSONRPCError", "code": 101, "message": "ERROR_MALFORMED_JSON" } ``` This is likely because the content length header is incorrect. When instead using `Content-Length: Buffer.byteLength(request, 'utf8')`, the request is accepted. --- src/request/NodeJsHttpRequester.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/request/NodeJsHttpRequester.ts b/src/request/NodeJsHttpRequester.ts index 44074fd..5862e8b 100644 --- a/src/request/NodeJsHttpRequester.ts +++ b/src/request/NodeJsHttpRequester.ts @@ -16,7 +16,7 @@ export class NodeJsHttpRequester implements HttpRequester { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Content-Length': request.length, + 'Content-Length': Buffer.byteLength(request, 'utf8'), }, }, response => {