1

I am defining the Content-Length of an HTTP request but because sometimes it might contain chinese characters when it gets to the response side of things, it gets truncated.

I have been trying to get the right way to set the content-length of a mixed Chinese/Latin String but with no luck. it keeps getting truncated.

headers["Content-Length"] = data.length;   //fails
{ \"query\": \"周杰伦\", \"type\": \"artist\"} //gets truncated

{ \"query\": \"gennaro\", \"type\": \"artist\"} //successful request

Can anyone suggest how to calculate the second string length for the content-length header properly?

NOTE: the body is stringified from JSON before it is sent over.

Thanks

Gabs
  • 137
  • 3
  • 10
  • [content length double byte](https://www.google.com/search?q=content-length+double+byte+site:stackoverflow.com) – mplungjan Feb 08 '22 at 13:29
  • @mplungjan what do you mean by that? – Gabs Feb 08 '22 at 13:30
  • Click it for similar questions and their answers – mplungjan Feb 08 '22 at 13:30
  • And I assume you did `headers["Content-Length"] = data.length; ` AFTER the json_encode – mplungjan Feb 08 '22 at 13:31
  • While I'm not sure what @connexo expects you to do with C# code, your question leaves a bit too much for imagination. How are you sending that data? Are you really expected to set the header yourself? Please provide relevant code portions, preferably an [mre]. – tevemadar Feb 08 '22 at 13:42
  • Unfortunately I picked the wrong duplicate. Here's the actual one: https://stackoverflow.com/questions/54369513/how-to-count-the-correct-length-of-a-string-with-emojis-in-javascript – connexo Feb 08 '22 at 14:05
  • Does this answer your question? [How to count the correct length of a string with emojis in javascript?](https://stackoverflow.com/questions/54369513/how-to-count-the-correct-length-of-a-string-with-emojis-in-javascript) – Heretic Monkey Feb 08 '22 at 14:06

1 Answers1

0

The fix this issue , I used TextEncoder which allow me to encode it to utf8 and then count it in the correct way.

let bodyLength = (new TextEncoder().encode(dataBody)).length;
Gabs
  • 137
  • 3
  • 10