Domanda

Here is a test Node.js application:

body = {
    "message": {
        "body": "hello", //it works
        // "body":"привет", //doesn't work
        "type":"TextMessage"
    }
};

body = JSON.stringify(body);

headers = {
    'Authorization': 'Basic ' + B64TOKEN,
    'Host': SUBDOMAIN + '.campfirenow.com',
    'Content-Type': 'application/json; charset=UTF-8',
    'Content-Length': body.length
}

opts = {
    host: SUBDOMAIN + '.campfirenow.com',
    port: 443,
    method: 'POST',
    path: '/room/' + TEST_ROOM + '/speak.json',
    headers: headers
}

request = require('https').request(opts, function(response) {
    var data;
    data = '';
    response.setEncoding('utf8');
    response.on('data', function(chunk) {
        return data += chunk;
    });
    return response.on('end', function() {
        console.log("===== start responce");
        console.log(data);
        console.log("===== end responce");
    });
  });

request.end(body);

body map is what I want to send. And you can see that with "hello" it work (ie message posted to Campfire chat) but with "привет" as body - an error occurs... In the second case i've got long html response from Campfire... I think this can be solved if I can send body in unicode sequence string... Like this: "body":"\u043f\u0440\u0438\u0432\u0435\u0442" but how?

È stato utile?

Soluzione 2

I'm (and you) should do this, if you need to respond with utf-8:

body = JSON.stringify(body);
buf = new Buffer(body);

headers = {
    'Authorization': 'Basic ' + B64TOKEN,
    'Host': SUBDOMAIN + '.campfirenow.com',
    'Content-Type': 'application/json; charset=UTF-8',
    'Content-Length': buf.length
}
// ...
request.end(buf);

You need to use Buffer. This way it works just fine.

Altri suggerimenti

Hej request.end takes a encoding argument http://nodejs.org/docs/v0.4.12/api/streams.html

set it to something supporting your characters.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top