0

I have an AngularJS app trying to submit a form to a Java backend deployed in a Tomcat 7.0.54. My AngularJS app seems to be submitting the form correctly. This is the content type header as recored by Chrome's inspector:

Content-Type:application/json;charset=UTF-8

The request payload, once again as recorded by Chrome's inspector, is:

{"newProject":{"title":"título","deadline":"30/Maio/2014", .....

That is, the AngularJS app is putting the request correctly in the wire. However, I',m unable to read this payload correctly in the server side. Characters like "í" are being printed as "?".

Just for the purpose of testing I modified my second filter in the chain (the first is Spring Security) for printing the content of the request. This is to be sure that neither my server side application nor any of the frameworks I'm using are interfering in my data.

@Override
    public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
        try {
            HttpServletRequest hsr = (HttpServletRequest)request;
            if( "POST".equalsIgnoreCase( hsr.getMethod() ) && "http://localhost:8080/profile/createproject".equalsIgnoreCase( hsr.getRequestURL().toString() ) ) {
                hsr.setCharacterEncoding( "UTF-8" );
                BufferedReader reader =  new BufferedReader( new InputStreamReader( request.getInputStream(), "UTF-8" ) );
                System.out.println( reader.readLine() );
            }

            chain.doFilter( request, response );

        } finally {
            MDC.remove( CHAVE_ID_REQUEST );

        }
    }

Even reading the request in the second filter of the chain I'm getting "t?tulo" instead of "título". If the same AngularJS app submits to a node backend, then the payload is correctly read and printed in the terminal.

Does anyone have any glue about the reason Tomcat can't read my UTF-8 request correctly?

Loreno Oliveira
  • 337
  • 1
  • 8
  • 19
  • 1
    Are you sure that System.out.println() will render UTF-8 correctly on your system? If you read the input stream directly (as you are doing) Tomcat just passes the bytes along. It doesn't know or care about the characterset being used. – Mark Thomas May 26 '14 at 09:53
  • Good point Mark. This morning I tested exactly the same code in a linux box and everything worked as expected. So, I got back to my OSX box, where the problem was occurring, and tried to do some research to understand what was wrong since this issue didn't exist some days ago. I found a lot of references about encoding problems related to java on OSX boxes. I tried some changes which didn't had any effect. My last attempt was changing, in Eclipse's configs, the default workspace encoding from the default ASCII to UTF-8 and now everything is ok again. Thank you for the hint! – Loreno Oliveira May 27 '14 at 01:46

1 Answers1

0

Seems like you already solved your particular problem, but wanted to add that a key reference for this kind of issue is http://wiki.apache.org/tomcat/FAQ/CharacterEncoding, which is referenced in https://stackoverflow.com/a/470320/830737

In particular, I was able to solve a similar issue by setting URIEncoding="UTF-8" in my <Connector> in server.xml.

Community
  • 1
  • 1
carueda
  • 182
  • 1
  • 6