1

我正在尝试将文档插入 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。

4

1 回答 1

6

因为您正在编写'data\n'请求的主体,这确实不是有效的 JSON。

大概,你的意思是:

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

body而不是将此作为选项的参数传递。

于 2012-07-25T12:07:16.690 回答