Skip to content

Calculate correct content length for UTF-8 string#4

Open
cskeppstedt wants to merge 1 commit into
trustly:mainfrom
cskeppstedt:patch-2
Open

Calculate correct content length for UTF-8 string#4
cskeppstedt wants to merge 1 commit into
trustly:mainfrom
cskeppstedt:patch-2

Conversation

@cskeppstedt

Copy link
Copy Markdown
Contributor

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 (929 != 930). When instead using Content-Length: Buffer.byteLength(request, 'utf8'), we get the correct content lenght, and the request is accepted.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant