Set correct encoding in the response object

Hi all

I’m building an API with RESTHandler. Is there a way to set the correct encdoing when setting data to the response object with event.getResponse().setData()?
The resulting JSON output seems to not correctly encode the german umlaute.
The value I get back from the request context is correctly encoded, I checked that already.
Example of my JSON output with the incorrect Encoding, lastname should be Schäfer:

{
    "data": {
        "registrationComplete": true,
        "registrationData": {
            "FIRSTNAME": "Test",
            "EMAIL": "thisisatest@test.ch",
            "LASTNAME": "Schäfer"
        }
    },
    "messages": [],
    "pagination": {
        "maxRows": 0,
        "totalPages": 1,
        "offset": 0,
        "page": 1,
        "totalRecords": 0
    },
    "error": false
}

Thanks for your help.

There are a number of things that can cause page encoding to get messed up. You will need to set the encoding at the sever level. `cfprocessingdirective pageencoding=“utf-8"; Usually that is the default on Linux/Lucee.

If that data is coming from the database you will need to make sure any driver arguments for UTF-8 are present and that the column type the data is stored in is UTF-8 compatible.

Lastly, you would probably want to set a response header of Content-Type: text/json;charset=utf-8

Thanks for you answer.

I tried setting the Content-Type through the member function setContentType("text/json;charset=utf-8") without any luck though.

As for the data, it’s not coming from the DB and I get the correct encoding when dumping it into a file directly before setting the data in the repsone object. As for the server, I’m not using Lucee, but I’m using an Adobe Coldfusion server running from commandbox. I’m using Adobe CF because we’re currently running it on the production servers. Where do you exactly set cfprocessingdirective(pageencoding="utf-8");? I’m running into an error defining that in a componet or directly in the function inside of the component.

You would place that directive in the onRequestStart of Application.cfc.

So the text is coming from a file or the database? If the former, you may need to specify the encoding on the fileRead

Neither. It’s data I receive as body for a registration. If the registration is completed successfully I return some of the input information to display it later.
And that was actually the fault :man_facepalming: The data was returned correctly. I checked with loading the customer from the DB, which was returned in the correct encoding.
But the body I sent through postman, was not the correct content-type. Seems like when dumping to a file, it converts it to the correct encoding. Tricked myself there. Thank you for your help, it lead me to the right answer. I will mark your first answer as correct as it technically is correct for the question.