使用Express提取POST数据

时间:2012-06-12 17:49:43

标签: http post express

我看过node.js Extracting POST data

但这是我的问题,当我收到类似这样的HTTP请求时,如何使用Express 提取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

我似乎无法通过Express获取msg=hello键值对。

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

如何获取正文的内容?

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

5 个答案:

答案 0 :(得分:3)

你的问题是bodyParser没有处理'application / 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);

答案 1 :(得分:2)

如果你在配置中有这个:

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

答案 2 :(得分:0)

我认为您需要配置express才能使用bodyParser中间件。 app.use(express.bodyParser());

见快递documentation

它说:

  

例如,我们可以发布一些json,并使用bodyParser中间件回显json,该中间件将解析json请求主体(以及其他),并将结果放在req.body中

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

我希望这有帮助!

答案 3 :(得分:0)

它可能(不确定它依赖于什么,但它发生在我身上,可能是bodyParser)请求体的格式化方式使得您的JSON数据被ITSELF视为密钥中的密钥 - 值对,具有空白对应值。在这种情况下,对我有用的是先提取JSON对象,然后照常进行:

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

这可能是非常不理想的,但如果没有其他工作(我试图寻找更好的方法并且没有找到),这可能适合你。

答案 4 :(得分:0)

我正在发布xml,我得到的答案基于JSON输入。如果要显示xml的内容,请处理原始请求:

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

作为邮递员使用curl的例子:

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-xml将您的请求对象动态处理到json对象,之后您将无法再处理原始请求。 (你可以猜到在这之后几个小时被卡住的人......)

相关问题