简体   繁体   中英

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.

NODEJS always receive empty request body.

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.

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。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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