1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Did you try `options.headers['Content-Length'] = Buffer.byteLength(options.body);` instead? `string.length` returns number of characters, whereas `Buffer.byteLength()` returns number of bytes. UTF-8 characters can span multiple bytes. – mscdex Jan 06 '15 at 23:19
  • Just tried it - unfortunately this is also not working. – André Meier Jan 07 '15 at 06:47
  • Ok. It seems this is a bug with redmines api / the used rails version. Posting data as xml solves the issue, no problem with newer rails version. – André Meier Jan 07 '15 at 17:10

0 Answers0