MattDionis MattDionis - 1 year ago 99
Java Question

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',

Answer Source

Save your javascript file to utf8.

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

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

// 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ý"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download