简体   繁体   English

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

[英]Can read data without content-type?

If send data without content-type header, can i read data? 如果发送不带内容类型标题的数据,我可以读取数据吗?

I just tried to read in nodejs with bodyparser, but i could not read. 我只是尝试使用bodyparser读取nodejs,但无法读取。

NODEJS always receive empty request body. NODEJS始终接收空的请求正文。

Is there any way? 有什么办法吗?

Using body-parser you need to specify the content type, otherwise express (which I think it is what you are using) won't be able to read the body. 使用body-parser您需要指定内容类型,否则express(我认为这是您正在使用的内容)将无法读取body。

You can always access the raw body: 您始终可以访问原始正文:

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);
});

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM