Restify getting request.body when content-type is application/octet-stream

时间:2015-04-23 05:25:49

标签: node.js file-upload content-type restify

Does anyone know why setting content-type to application/octet-stream then request body is undefined in restify?

Here is my code

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 Request

POST /test 
Content-Type: text/plain

hello

Console print: hello

POST /test 
Content-Type: image/png

hello

Console print: <Buffer 68 65 6c 6c 6f>

POST /test 
Content-Type: application/octet-stream

hello

Console print: undefined

1 个答案:

答案 0 :(得分:0)

从我在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永远不会被分配......