如何读取node.js中的内容类型:应用程序/八位字节流请求
我已将post man的以下请求发送到node.js:如何读取node.js中的内容类型:应用程序/八位字节流请求,node.js,Node.js,我已将post man的以下请求发送到node.js: curl -X PUT -H "Content-Type: application/octet-stream" -H "app_key: dhdw-sdsjdbsd-dsdv-ddd" -H "Authorization: Login jddjd-ddd-ddd-ddd-ddd" -H "Cache-Control: no-cache" -H "Postman-Token: dbee5942-5d54-c1b7-415c-7ddf9cb88
curl -X PUT -H "Content-Type: application/octet-stream" -H "app_key: dhdw-sdsjdbsd-dsdv-ddd" -H "Authorization: Login jddjd-ddd-ddd-ddd-ddd" -H "Cache-Control: no-cache" -H "Postman-Token: dbee5942-5d54-c1b7-415c-7ddf9cb88cd0" -d 'Code,Name,Parent,Address
root,root,,root
root1,root1,root,root1
root2,root2,root1,root2
root3,root3,root2,root3
root4,root4,root,root4
root5,root5,root4,root5
root6,root6,root,root6
' http://localhost:9000/api/locations/import
如何处理node.js中的上述请求并读取请求数据
上述请求击中了我的路由器:
app.put('api/locations/import', function(req,res){
'use strict';
console.log(req.body);
console.log(req.body.hello);
res.send(200);
});
我总是得到req.body
是{}
。但我期待着:
root,root,,root
root1,root1,root,root1
root2,root2,root1,root2
root3,root3,root2,root3
root4,root4,root,root4
root5,root5,root4,root5
root6,root6,root,root6
根,根,根
根1,根1,根,根1
根2,根2,根1,根2
根3,根3,根2,根3
根4,根4,根,根4
根5,根5,根4,根5
根6,根6,根,根6
为了解决这个问题,我做了如下工作 首先,我在应用程序级别中添加以下设置
var getRawBody = require('raw-body');
app.use(function (req, res, next) {
if (req.headers['content-type'] === 'application/octet-stream') {
getRawBody(req, {
length: req.headers['content-length'],
encoding: req.charset
}, function (err, string) {
if (err)
return next(err);
req.body = string;
next();
})
}
else {
next();
}
});
之后,当我从下面的代码中读取数据时
app.put('api/locations/import', function(req,res){
'use strict';
console.log(req.body);
console.log(req.body.hello);
res.send(200);
});