4

我读过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

我似乎无法msg=hello使用 Express 将键值对从正文中取出。

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

如何获取body的内容?

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

5 回答 5

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);
于 2013-01-02T16:10:05.737 回答
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)
});
于 2012-06-15T15:21:24.620 回答
0

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

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

这可能不是最理想的,但如果没有其他方法(我尝试了很长时间试图找到更好的方法但没有找到),这可能对你有用。

于 2013-05-27T11:34:33.020 回答
0

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

请参阅快速文档

它说:

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

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

我希望这有帮助!

于 2012-06-12T18:36:08.703 回答
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 中间件不会妨碍您:body-parser-xml 将您的请求对象即时处理为 json 对象,之后您将无法再处理您的原始请求。(你可以猜到谁在这之后几个小时被卡住了......)

于 2017-12-06T10:41:32.967 回答