有谁知道为什么将内容类型设置为application / octet-stream然后在resify中未定义请求正文?
这是我的代码
var restify = require('restify');
var server = restify.createServer({});
server.use(restify.bodyParser({ }));
server.post('/test', function(req, res, next){
console.log(req.body);
});
server.listen(8090, function() {});
HTTP请求
POST /test
Content-Type: text/plain
hello
控制台打印:您好
POST /test
Content-Type: image/png
hello
控制台打印:
POST /test
Content-Type: application/octet-stream
hello
控制台打印:未定义
最佳答案
从我在restify中可以看到,这是由于bodyParser仅期望从https://github.com/restify/node-restify/blob/master/lib/plugins/json_body_parser.js#L28中提取的Content-Type: application/json
if (req.getContentType() !== 'application/json' || !req.body) {
next();
return;
}
所以req.body永远不会被分配...