可以读取没有内容类型的数据吗?

Can read data without content-type?

如果发送的数据没有content-type header,我可以读取数据吗?

我刚刚尝试用 bodyparser 在 nodejs 中读取,但我无法读取。

NODEJS 总是收到空请求体。

有什么办法吗?

您需要将内容类型添加为 application/json,以便正文解析器将数据解析为 json。

使用body-parser你需要指定内容类型,否则express(我认为这是你正在使用的)将无法读取正文。

您随时可以访问原始正文:

app.use (function(req, res, next) {
    var data='';
    req.setEncoding('utf8');
    req.on('data', function(chunk) { 
       data += chunk;
   });

   req.on('end', function() {
       req.body = data;
       next();
   });
});

app.post('/', function(req, res) {
    // you have set the body before:
    console.log(req.body);
});