我正在将POST数据从Python程序发送到Node.JS服务器,并以res.end返回.这是python代码:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import requests
value = u"Этот текст в кодировке Unicode"
url = "http://localhost:3000/?source=test"
headers = {'content-type': 'text/plain; charset=utf-8'}
r = requests.post(url, data=value.encode("utf-8"))
print r.text
这是我在Node.JS中处理数据的方式:
http.createServer(function(req, res) {
req.setEncoding = "utf8"
var queryData = '';
if (req.method == 'POST') {
req.on('data', function(data) {
queryData += data;
});
req.on('end', function() {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end(queryData)
});
} else {
// sending '405 - Method not allowed' if GET
res.writeHead(405, {
'Content-Type': 'text/plain'
});
res.end();
}
}).listen(3000, '127.0.0.1');
结果我得到:
$python test.py
?????? ??????? ?? ????????????????? Unicode
结果如何正确设置编码以获取“ЭтоттекствкодировкеUnicode”?谢谢.