UTF-8 fix breaks content-type headers? | Community
Skip to main content
Level 8
May 25, 2021

UTF-8 fix breaks content-type headers?

  • May 25, 2021
  • 2 replies
  • 4850 views

By default, AEM queryBuilder, which is how servlets can lookup content, responds with the data encoded in iso-8859 char set instead of the required UTF-8.  The solution, which seems improbable, but works, is to create /osgiconfig/config/org.apache.sling.engine.impl.SlingMainServlet.cfg.json file with the following content:

 

 

{ "sling.additional.response.headers":[ "X-Content-Type-Options=nosniff", "Content-Type=text/html;charset=utf-8", "X-Frame-Options=SAMEORIGIN" ] }

 

 

This fixes the queryBuilder returning data with the wrong charset in the servlet, but has the side effect of making the response header always "text/html".  We want our servlets, which are used by the front end to search for and pull page pages as json, to have a content-type of application/json.  however, if we change the above file, it will then break AEM.

We tried overriding the content type in the servlet itself, but this gets ignored (or overwritten).

Any ideas how to support both UTF-8 and application/json just for our servlets?

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

Asutosh_Jena_
Community Advisor
Community Advisor
May 25, 2021

Hi @tb3dock 

 

Please check "Apache Sling Request Parameter Handling" configuration in /system/console/configMgr and see what value "sling.default.parameter.encoding" property is holding. If it's set to "ISO-8859-1" please change it to "UTF-8"

Default value should be "UTF-8".

TB3dockAuthor
Level 8
May 25, 2021
Hi this is for parameter encoding, so has nothing to do with the servlet response I believe. This value was set to UTF-8 when we started.
January 10, 2022

+1 having the same issue. Could solve this using the code below (be sure to maintain the order! doesn't take the value otherwise):

response.setStatus(statusCode);
response.setContentType(ContentTypeUtil.TYPE_JSON);
response.setCharacterEncoding(CharEncoding.UTF_8);
response.getWriter().write(json.toString());
Adobe Employee
February 24, 2022

mauricio_rodri2 

Thanks for the solution I had the same issue and resolved with your answer 🙂