Problems with http-post requests with German umlauts (ä ö ü) or special characters (ß) with Node.js

TL; DR; resolved, see comments below :)

I'm really crazy: I want to publish JSON in the Redmine API to send time records imported through CSV files. Everything works well until I try to send strings with German umlauts (ä ü ö) or special characters (ß).

I am using a query package with the following code:

var options = { url: self.getHost() + path, headers: { 'Content-Type': 'application/json; charset=utf-8' }, method: method }; options.headers[self.getApiKeyHeader()] = self.getApiKey(); if (method !== HttpMethods.Get) { params.time_entry.comments = 'äöüß'; options.body = JSON.stringify(params); options.headers['Content-Length'] = options.body.length; } 

And send it like this:

 request(options, function (error, response) { if (response.statusCode === 201) { console.log('Success'); } else { console.log('Error: ' + response.statusCode); } }); 

I always get an HTTP500 error message with server logs that say "Invalid byte sequence in UTF-8." I get the same error when I try to post JSON via Postman. Since my colleagues have no problems with Ruby and PHP script, I assume that I have something terrible in my script. Also tried the following alternatives for setting options.body content:

 options.body = new Buffer(JSON.stringify(params), encoding='utf8'); options.body = new Buffer(JSON.stringify(params, 'ascii').toString('utf8'); 

Both do not work either.

I use https://www.npmjs.com/package/request for requests.

What am I doing wrong?

+1
utf-8 request
source share

All Articles