Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforce utf8 encoding in call from node to Java

I am making a call from a node middle-tier to a Java backend and passing a string as a query param. Everything works great until non-English alphabet character are used (ex: ř,ý). When Java receives these characters it throws:

parse exception: org.eclipse.jetty.util.Utf8Appendable$NotUtf8Exception: Not valid UTF8!

This call works perfectly:

GET http://localhost:8000/server/name?name=smith

This call fails with the above error:

GET http://localhost:8000/server/name?name=sořovský

My question involves where to address this issue. I have found this utf8 encoder for node and was thinking about encoding my strings as utf8 before calling my Java layer in the future. Is that the correct approach or should I be doing something within Java?

Note, this is what my relevant request headers look like:

{
  ...
  accept: 'application/json, text/plain, */*',
  'accept-encoding': 'gzip, deflate, sdch',
  'accept-language': 'en-US,en;q=0.8,el;q=0.6',
  ...
}
like image 926
MattDionis Avatar asked Aug 31 '16 14:08

MattDionis


2 Answers

Save your javascript file to utf8.

var name = "sořovský",
    param1 = encodeURIComponent(name);

var url = "http://localhost:8000/server/name?name=" + param1;

console.log(url);
// http://localhost:8000/server/name?name=so%C5%99ovsk%C3%BD

You can see the log with GET http://localhost:8000/server/name?name=sořovský :

{
  "args": {
    "name": "sořovský"
  }, 
  "headers": {
    "Accept": "application/json, text/plain, */*",
    "Accept-encoding": "gzip, deflate, sdch",
    "Accept-language": "en-US,en;q=0.8,el;q=0.6",
    //...
  },
  "url": "http://localhost:8000/server/name?name=sořovský"
}
like image 150
Sky Voyager Avatar answered Nov 07 '22 04:11

Sky Voyager


GET only supports the ASCII char.set to send other characters you need to percent encode the special chars.

like image 37
Nicklas Andersson Avatar answered Nov 07 '22 05:11

Nicklas Andersson