TLDR; solved, see comments below :)
I am really getting nuts with this: I want to post JSON to the Redmine API to send time entries imported via CSV files. Everything works well, until I try to send strings with German umlauts (ä ü ö) or special chars (ß).
I am using the request 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 with server logs saying "Invalid byte sequence in UTF-8". I get the same error when I try to post the JSON via Postman. As my coworkers have no problems with Ruby and PHP script, I guess I got something terribly wrong in my script. Also tried the following alternatives for setting the options.body content:
options.body = new Buffer(JSON.stringify(params), encoding='utf8');
options.body = new Buffer(JSON.stringify(params, 'ascii').toString('utf8');
With both also not working.
I am using https://www.npmjs.com/package/request for the requests.
What am I doing wrong?