I am rewriting a small python script in node.js. The original script worked like this:
# -*- coding: utf-8 -*-
import urllib
import httplib
import json
def rpc(url, args = { }):
try:
post_data = json.dumps({'args': args})
f = urllib.urlopen(url, post_data)
if not f or f.code != 200:
return { 'result': 1, 'error': 'urlopen returned error' }
data = f.read()
js_data = json.loads(data)
except Exception, e:
return { 'result': 2, 'error': e }
else:
return { 'result': 0, 'data': js_data }
print rpc('http://server.local/rpc', {'x': u'тест'})
I use request to do the same in node.js:
var request = require('request')
request.post('http://server.local/rpc', {
json: {'x': 'тест'}
}, function(err, result) {
console.log(err, result.body)
})
It works, but the unicode data is garbled, so that I get ÑеÑÑ
instead of тест
when querying the data back.
It seems strange, given that both python and node.js should be sending utf8-encoded data.
Btw, the server is written in perl, I think, but that's all I know about it :(
Also, server returns unicode data on other queries, so it is able to do that.
Upd. my console prints unicode characters fine.
Upd. Rewrote my code to use node.js http
module:
var http = require('http')
var options = {
hostname : 'server.local',
path : '/rpc',
method : 'POST'
}
var req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
var body = JSON.stringify({'x': 'тест'})
req.setHeader('Content-length', body.length)
// python sends data with this header
req.setHeader('Content-type', 'application/x-www-form-urlencoded')
req.on('error', function (e) {
console.log('problem with request: ' + e);
});
req.end(body, 'utf8');
The results are sadly the same. Also same behavior on two different installations (my personal MBA and production Debian server). So it does seem to be something with the way node.js represents unicode data.