2014-10-31 73 views
3

我已將以下請求從郵遞員到的node.js:如何讀取內容類型:在node.js的應用/八位字節流請求

curl -X PUT -H "Content-Type: application/octet-stream" -H "app_key: dhdw-sdsjdbsd-dsdv-ddd" -H "Authorization: Login jddjd-ddd-ddd-ddd-ddd" -H "Cache-Control: no-cache" -H "Postman-Token: dbee5942-5d54-c1b7-415c-7ddf9cb88cd0" -d 'Code,Name,Parent,Address 

root,root,,root 
root1,root1,root,root1 
root2,root2,root1,root2 
root3,root3,root2,root3 
root4,root4,root,root4 
root5,root5,root4,root5 
root6,root6,root,root6 
' http://localhost:9000/api/locations/import 

如何可以處理來自節點上述請求.js並讀取請求數據?

上述要求打我的路由器:

app.put('api/locations/import', function(req,res){ 
    'use strict'; 
    console.log(req.body); 
    console.log(req.body.hello); 
    res.send(200); 
}); 

我總是讓req.body{}。但我期待:

 
root,root,,root 
root1,root1,root,root1 
root2,root2,root1,root2 
root3,root3,root2,root3 
root4,root4,root,root4 
root5,root5,root4,root5 
root6,root6,root,root6 
+0

你能詳細點嗎? – majidarif 2014-10-31 11:15:03

回答

1

我做了以下我從下面的代碼

讀取數據來解決這個問題

拳我添加下面的應用級

var getRawBody = require('raw-body') 
app.use(function (req, res, next) { 
     if (req.headers['content-type'] === 'application/octet-stream') { 
      getRawBody(req, { 
       length: req.headers['content-length'], 
       encoding: this.charset 
      }, function (err, string) { 
       if (err) 
        return next(err) 

       req.body = string 
       next() 
      }) 
     } 
     else { 
      next() 
     } 

    }) 

設置之後

app.put('api/locations/import', function(req,res){ 
    'use strict'; 
    console.log(req.body); 
    console.log(req.body.hello); 
    res.send(200); 
}); 
+0

這看起來像ExpressJs代碼,而不是RequestJs – Tim 2016-03-07 22:56:46

相關問題