Node.js请求库 - 将text/xml发布到body?

Yab*_*rgo 4 request node.js express

我正在尝试设置一个简单的node.js代理来将帖子传递给Web服务(CSW in this isntance).

我在请求体中发布XML,并指定text/xml. - 服务需要这些.

我在req.rawBody var中得到原始的xml文本并且它工作正常,但我似乎无法正确地重新提交它.

我的方法看起来像:

app.post('/csw*', function(req, res){


  console.log("Making request to:"  + geobusOptions.host + "With query params: " + req.rawBody);


request.post(
    {url:'http://192.168.0.100/csw',
    body : req.rawBody,
    'Content-Type': 'text/xml'
    },
    function (error, response, body) {        
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);
});
Run Code Online (Sandbox Code Playgroud)

我只想在POST中使用content-type text/xml提交一个字符串.但我似乎无法实现这一目标!

我正在使用'request'库@ https://github.com/mikeal/request

编辑 - 哎呀!我忘了添加标题...

这非常有效:

request.post(
    {url:'http://192.168.0.100/csw',
    body : req.rawBody,
    headers: {'Content-Type': 'text/xml'}
    },
    function (error, response, body) {        
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);
Run Code Online (Sandbox Code Playgroud)

Yab*_*rgo 7

好吧,我最终想出来了,重新发布一个nodeJS代理请求的正文,我有以下方法:

request.post(
    {url:'http://192.168.0.100/csw',
    body : req.rawBody,
    headers: {'Content-Type': 'text/xml'}
    },
    function (error, response, body) {        
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    }
);
Run Code Online (Sandbox Code Playgroud)

我使用以下代码获取rawbody:

app.use(function(req, res, next) {
  req.rawBody = '';
  req.setEncoding('utf8');

  req.on('data', function(chunk) { 
    req.rawBody += chunk;
  });

  req.on('end', function() {
    next();
  });
});
Run Code Online (Sandbox Code Playgroud)