发送到CouchDB时,Json格式不正确

在使用node.js作为代理来防止xss问题时,我有一个向前发送json的问题。

logging收到的数据时,我找不到任何问题。

当我编写另一个显示接收到的数据的node.js服务器时,解决scheme就来了,我让它模仿了CouchDB服务器。

事实certificate,这是一个非ASCII字符(瑞典Å)是罪魁祸首。 收到的数据被视为原始计算内容长度严重,或根据你的心情正确。 ;)

解决scheme是在计算Content-Length之前使用Buffer将原始数据转换为utf8。

: if (request.method == 'PUT') { var data = ''; request.on('data', function(dataSnippet) { data += dataSnippet; if (data.length > 1e6) {request.connection.destroy();} }); request.on('end', function(dataSnippet) { data = new Buffer(data, 'UTF8'); //<--- This is the solution options.headers = { 'Content-Encoding': 'UTF8', 'Content-Type': 'application/json', 'Content-Length': data.length //<--- Where it went wrong } proxy = http.request(options, function(proxy_response) { proxy_response.setEncoding('UTF8'); proxy_response.pipe(response); }); proxy.write(data); proxy.end(); }); } :