使用Express提取POST数据

问题描述:

我读过node.js Extracting POST data使用Express提取POST数据

但是,这是我的问题,当我收到HTTP请求看起来像这样,如何提取POST数据与快递

POST /messages HTTP/1.1 
Host: localhost:3000 
Connection: keep-alive 
Content-Length: 9 
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5 
Content-Type: application/xml 
Accept: */* 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: zh-TW,zh;q=0.8,en-US;q=0.6,en;q=0.4 Accept-Charset: UTF-8,*;q=0.5 

msg=hello 

我似乎无法得到msg=hello键值对出身体与快递的。

我试过所有这些方法req.header()req.param()req.query()req.body但他们似乎是空的。

如何获取人体内容?

app.post('/messages', function (req, res) { 
    req.?? 
}); 

我相信你需要配置express以使用bodyParser中间件。 app.use(express.bodyParser());

查看快递documentation

它说:

例如,我们可以张贴一些JSON和呼应回JSON使用bodyParser中间件将解析JSON请求主体(以及其他),并且将结果放在REQ。 body

req.body()现在应该返回预期的帖子正文。

我希望这有助于!

+0

谢谢!是的,我已经安装了'bodyParser',但'req.body'对象仍然是空的:o – elliot

+0

hmm有趣。你确定你正在发送一个有效的请求体?你有没有尝试提交一些json到'/ messages'路径来测试? –

如果您在配置有这样的:

app.use(express.bodyParser());

这在您的视图:

form(name='test',method='post',action='/messages') 
    input(name='msg') 

那么这应该工作:

app.post('/messages', function (req, res) { 
    console.log(req.body.msg); 
    //if it's a parameter then this will work 
    console.log(req.params.msg) 
}); 

你的问题是bodyParser不处理“应用程序/ XML”,我解决了这个主要是通过阅读这篇文章:https://groups.google.com/forum/?fromgroups=#!topic/express-js/6zAebaDY6ug

你需要编写自己的解析器,我已经发布了更多的细节给github:

https://github.com/brandid/express-xmlBodyParser

var utils = require('express/node_modules/connect/lib/utils', fs = require('fs'), xml2js = require('xml2js'); 

function xmlBodyParser(req, res, next) { 
    if (req._body) return next(); 
    req.body = req.body || {}; 

    // ignore GET 
    if ('GET' == req.method || 'HEAD' == req.method) return next(); 

    // check Content-Type 
    if ('text/xml' != utils.mime(req)) return next(); 

    // flag as parsed 
    req._body = true; 

    // parse 
    var buf = ''; 
    req.setEncoding('utf8'); 
    req.on('data', function(chunk){ buf += chunk }); 
    req.on('end', function(){ 
     parser.parseString(buf, function(err, json) { 
      if (err) { 
       err.status = 400; 
       next(err); 
      } else { 
       req.body = json; 
       next(); 
      } 
     }); 
    }); 
} 

然后用

app.use (xmlBodyParser); 

有可能使用它(不知道这取决于,但它发生在我身上一次,它可能是bodyParser)该请求正文的格式使得您的JSON数据被ITSELF视为键值对中的键,并具有空白对应值。什么工作对我来说在这种情况下是先提取JSON对象,然后进行正常:

var value; 
for (var item in req.body) 
{ 
    var jObject = JSON.parse(item); 
    if (jObject.valueYouWant != undefined) 
    { 
     value = jObject.valueYouWant; 
    } 
} 

这可能是非常不理想的,但如果没有其他作品(我想了好半天试图找到一种更好的方式找不到)这可能适合你。

您正在发布xml,因为我看到,您得到的答案是基于JSON输入。使用curl作为您的邮递员

app.post('/processXml',function (req, res) 
{ 
    var thebody = ''; 
    req.on('data' , function(chunk) 
    { 
     thebody += chunk; 
    }).on('end', function() 
    { 
     console.log("body:", thebody); 
    }); 
}); 

举个例子:如果你希望你的XML显示的内容,处理原始请求

curl -d '<myxml prop1="white" prop2="red">this is great</myxml>' -H 
"Content-type: application/xml" -X POST 
http://localhost:3000/processXml 

对外输出:

'<myxml prop1="white" prop2="red">this is great</myxml>' 

确保您的body-parser中间件不会妨碍:body-parser-xml将您的请求对象即时处理为json对象,之后您无法再处理原始请求。 (你可以猜出谁在这之后几个小时被卡住了......)