Skip to content Skip to sidebar Skip to footer

Node.js / Express - How Do I Set Response Character Encoding?

Say I got: app.get('/json', function(req, res) { res.set({ 'content-type': 'application/json' }).send('{'status': '0'}'); }); I'm trying to send the response as UT

Solution 1:

You will probably want to explicitly add a charset to the end of your content-type string if you find it's not being set already by Express:

 res.set({ 'content-type': 'application/json; charset=utf-8' });

The charset is not always set automagically and does need to be set to work correctly everywhere (i.e. with all browsers and all ajax libraries) or you can run into encoding bugs.

In Express 4.x specifically I've found that depending on the object you trying to return, it normally automatically returns with content-type: application/json; charset=utf-8 when you call res.json(someObject), however not always.

When calling res.json() on some objects it can return content-type: application/json (i.e. without the charset encoding!). I'm not actually sure what triggers this, other than it's something about the specific object being returned.

I've only noticed it because of automated tests which explicitly checked the headers and found it was missing the charset declaration on some responses (even though the content-type was still application/json).

Solution 2:

Use res.charset: http://expressjs.com/api.html#res.charset

res.charset = 'value';
res.send('some html');
// => Content-Type: text/html; charset=value

However, JSON is UTF-8 by default so you don't need to set anything.

Solution 3:

This worked for me

res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});

Solution 4:

Having similar issues I'm collecting Swedish characters from a database and outputting them as JSON object, node doesn't really care if json must be UTF-8 or not when the chars from the database isn't in UTF-8.. So assuming "you don't need to set anything" is false. Depending on what charsets you are working with.

Solution 5:

Before you go to the trouble of manually setting header parameters, check what your server is already sending by default. In my case, I'm using a "serverless" cloud provided Node.js instance. Apparently, these are usually front-ended w/ NGINX which I assume is what sets some of this stuff based on default settings. ...I didn't need to res.set anything at all. Granted, I'm serving back HTML, ...just sayin - before you go fixin, make sure it's broke.

accept-ranges:bytesaccept-ranges:bytescache-control:privatecontent-encoding:gzipcontent-type:text/html;charset=utf-8date:Fri,21Dec2018 21:40:37 GMTetag:W/"83-xwilN/BBLLLAAAHHH/0NBLAH0U"function-execution-id:5thvkjd4wwruserver:nginxstatus:200vary:accept-encoding,cookie,authorizationvia:1.1varnishx-cache:MISSx-cache-hits:0x-cloud-trace-context:18c611BBBBLLLLAAAHHH9594d9;o=1x-powered-by:Expressx-served-by:cache-dfw18631-DFWx-timer:S15BBLLLAAHHH.913934,VS0,VE3404

Post a Comment for "Node.js / Express - How Do I Set Response Character Encoding?"