Note that there are some explanatory texts on larger screens.

plurals
  1. POUTF-8 Strings getting scrambled by Restlet on GAE
    text
    copied!<p>I have a simple Restlet service hosted on AppEngine. This performs basic CRUD operations with strings and is working well with all sorts of UTF-8 characters when I test it with curl (for all the verbs).</p> <p>This is consumed by a simple restlet client hosted in a servlet on another AppEngine app:</p> <pre><code>// set response type resp.setContentType("application/json"); // Create the client resource ClientResource resource = new ClientResource(Messages.SERVICE_URL + "myentity/id"); // Customize the referrer property resource.setReferrerRef("myapp"); // Write the response resource.get().write(resp.getWriter()); </code></pre> <p>The above is pretty much all I have in the servlet. Very plain.</p> <p>The servlet is invoked via jquery ajax, and the json that I get back is well formed and everything, but the problem is that UTF-8 encoded strings are coming back scrambled, for example: <code>Université de Montréal</code> becomes <code>Universit?? de Montr??al</code>. </p> <p>I tried adding this line in the servlet (before everything else): </p> <pre><code>resp.setCharacterEncoding("UTF-8"); </code></pre> <p>But the only diference is that instead of getting <code>??</code> I get <code>Universitᅢᄅ de Montrᅢᄅal</code> (I don't even know what kind of characters those are, asian I suppose).</p> <p>I am 100% sure the restlet service is OK, because other than debugging it line by line I am able to test it from cmd line with curl and it's returning well formed strings.</p> <p>By looking at the http header of the response from firefox (when calling the servlet via javascript) I can see the encoding is indeed UTF-8, as expected. After hours of struggling reading every possible related article I came across <a href="http://www.mail-archive.com/discuss@restlet.tigris.org/msg14211.html" rel="noreferrer">this restlet discussion</a> and noticed that indeed I do have <code>Transfer-Encoding: chunked</code> on the http header of the response. I tried the proposed solutions (override ClientResource.toRepresentation, didn't do any good so I tried restlet 2.1 as susggested with <code>ClientResource.setRe​questEntityBuffering​(true)</code>, no luck there either) but <strong>I am not convinced my issue is related to</strong> <code>Transfer-Encoding: chunked</code> <strong>at all</strong>.</p> <p>At this point I am out of ideas, and I would <strong>really</strong> appreciate any suggestions! O_o</p> <p><strong>UPDATE</strong>:</p> <p>I tried doing a manual GET with a classic UrlConnection and the string is coming back alright:</p> <pre><code>URL url = new URL(Messages.SERVICE_URL + "myentity/id"); URLConnection conn = url.openConnection(); InputStream is = conn.getInputStream(); StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); resp.getWriter().print(writer.toString()); </code></pre> <p>So much for being all RESTful and fancy ...but still I have no clue why the original version doesn't work! :/</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload