DSb*_*ard 1 json couchdb node.js
我正在尝试将文档插入CouchDB.执行此代码时,CouchDB返回以下错误:
STATUS: 400
BODY: {"error":"bad_request","reason":"invalid_json"}
Run Code Online (Sandbox Code Playgroud)
我的代码:
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();
Run Code Online (Sandbox Code Playgroud)
怎么了?
编辑:我需要更新数据,所以我将POST替换为PUT.
因为您正在编写'data\n'请求的正文,而且这确实不是有效的JSON.
可能,你的意思是:
req.write(JSON.stringify({"data": "somedata"}));
Run Code Online (Sandbox Code Playgroud)
而不是将其作为body选项的参数传递.
| 归档时间: |
|
| 查看次数: |
3697 次 |
| 最近记录: |