尝试从Node.js插入文档到CouchDB时,出现“bad_request invalid_json”错误

我正在尝试将文档插入到CouchDB中。 在执行此代码时,CouchDB返回以下错误:

STATUS: 400 BODY: {"error":"bad_request","reason":"invalid_json"} 

我的代码:

 var http = require('http') var options = { "host": "localhost", "port": "5984", "path": "/chinese", "headers": {"content-type": "application/json"}, "method": "PUT", "body": JSON.stringify({ "_id":"rabbit", "_rev":"2-c31d8f403d44d1082b3b178ebef8d329", "Subject":"I like Plankton" }) }; var req = http.request(options, function(res) { console.log('STATUS: ' + res.statusCode); res.setEncoding('utf8'); res.on('data', function (chunk) { console.log('BODY: ' + chunk); }); }); req.write('data\n'); req.end(); 

怎么了?

编辑:我需要更新数据,所以我把POST换成了PUT。

因为你正在写'data\n'作为你的请求的主体,而且这不是真正有效的JSON。

可能你的意思是:

 req.write(JSON.stringify({"data": "somedata"})); 

而不是将其作为选项的bodyparameter passing。