节点JS.发送带有XML数据的帖子 [英] Node JS. Send post with XML data

查看:168
本文介绍了节点JS.发送带有XML数据的帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发送带有一些原始XML数据的发布请求.但是,它不起作用.下面的代码不发送正文,就像我不发送数据一样. 有人有任何线索如何实现这一目标吗?尝试了不同的模块,似乎没有一个能够发送原始XML.

I am trying to send a post request with some raw XML data. However it's not working. The code below sends no body, as if I send no data. Anybody has any clue how to achieve this? Tried different modules, none seemed to be able to send raw XML.

var http = require('http');
var querystring = require('querystring');

var postData = '<?xml version="1.0" encoding="utf-8" standalone="yes"?><SearchPrices><City Code="19333" /></SearchPrices>';

var options = {
  hostname: 'postcatcher.in',
  port: 80,
  path: '/catchers/553133a9acde130300000f08',
  method: 'POST',
  headers: {
    'Content-Type': 'application/xml',
    'Content-Length': postData.length
  }
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write(postData);
req.end();

想法受到赞赏

这是一个成功使用此服务的PHP代码.

Here is a PHP code that uses this service successfully.

$xml_data ='<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<SearchPrices>
  <City Code="19333" />
  <PaxNationality Code="AR" />
  <Language Code="ES" />
  <CheckInDate><![CDATA[2015-04-21]]></CheckInDate>
  <CheckOutDate><![CDATA[2015-04-23]]></CheckOutDate>
  <IncludeRoomName />
  <IncludeCancellationCharges />
  <IncludePromoName />
  <IncludeGeorefInfo />

 <RoomsCriteria>
    <Room Number="1">
      <Adults>2</Adults>
    </Room>

  </RoomsCriteria>
</SearchPrices>
';


$URL = "URL";

$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING , 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/html', 'Authorization: '.$encodeText, 'Accept-Encoding: deflate'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

print_r($output);
enter code here

推荐答案

这是一个可行的示例.

var request = require('request');
var xml2js = require('xml2js');
var parser = new xml2js.Parser({explicitArray: false, trim: true});


function(url, xml, cb) {
    var self = this;

    var options = {
        method: 'POST',
        keepAlive: false,
        url: url,
        headers: {
          "Authorization": "Basic " + this.auth,
          'Content-Type': 'application/x-www-form-urlencoded',
          'Accept-Encoding': 'gzip',
        },
        body: xml,
        gzip: true //depending on the webservice, this has to be false
      };

      var x = request(options, function (error, response, body) {
        //I convert the response to JSON. This is not mandatory
        parser.parseString(body, function(err, result) {
          cb(null, result);
        });
      });
};

标题取决于您使用的Web服务.另外,如果网络服务提供了wsdl,那将是极大的缓解.

The headers will depend on the webservice you are using. Also, if the webservice provides wsdl, that's a huge relieve.

这篇关于节点JS.发送带有XML数据的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆