【问题标题】:how to post XML data in node.js http.request如何在 node.js http.request 中发布 XML 数据
【发布时间】:2012-12-10 16:39:40
【问题描述】:

我正在尝试使用 http.request 通过 Node.js 向 Web 服务提交 xml 请求。

这是我的代码。我的问题是我想将 xml 发布到服务而不是 data=1

http.request({
   host: 'service.x.yyy.x',
   port: 80,
   path: "/a.asmx?data=1",
   method: 'POST'
}, function(resp) {
   console.log(resp.statusCode);
   if(resp.statusCode) {
        resp.on('data', function (chunk) {
            console.log(chunk);
            str +=  chunk;                  
        });
        resp.on('end', function (chunk) {                           
            console.log(str);            
        });                   
  }
}).end();

怎么做?

【问题讨论】:

    标签: node.js post xmlhttprequest express


    【解决方案1】:
    var request = require("request");
    request.post({
        rejectUnauthorized: false,
        url: 'URL',
        method: "POST",
        headers: {
            'Content-Type': 'application/xml',
        },
        body: '<XML>'
    }, function (error, response, body) {
        if (error) {
            // Handle error
        } else {
            // Handle Response and body
        }
    });
    

    【讨论】:

      【解决方案2】:

      实际上Andrey Sidorov 提供的链接有助于使其正常工作。 这行得通。

      var body = '<?xml version="1.0" encoding="utf-8"?>' +
                 '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">'+
                  '<soap12:Body>......</soap12:Body></soap12:Envelope>';
      
      var postRequest = {
          host: "service.x.yyy.xa.asmx",
          path: "/a.asmx",
          port: 80,
          method: "POST",
          headers: {
              'Cookie': "cookie",
              'Content-Type': 'text/xml',
              'Content-Length': Buffer.byteLength(body)
          }
      };
      
      var buffer = "";
      
      var req = http.request( postRequest, function( res )    {
      
         console.log( res.statusCode );
         var buffer = "";
         res.on( "data", function( data ) { buffer = buffer + data; } );
         res.on( "end", function( data ) { console.log( buffer ); } );
      
      });
      
      req.on('error', function(e) {
          console.log('problem with request: ' + e.message);
      });
      
      req.write( body );
      req.end();
      

      【讨论】:

      • 我这样做了,但它显示“使用 POST 方法发送 'xml' 参数”。我在做什么
      【解决方案3】:

      http.request 返回ClientRequest 对象,它也是一个可写流。 而不是.end()end(xmlbody).write(xmlbody).end()

      【讨论】:

        猜你喜欢
        • 2021-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多