How to consume MTOM SOAP web service in node.js?

前端 未结 2 749
梦如初夏
梦如初夏 2021-01-14 08:14

I need to download or process a file from a soap based web service in node.js. can someone suggest me on how to handle this in node.js

I tried with \'node-soap\' or

2条回答
  •  逝去的感伤
    2021-01-14 08:47

    Use ws.js

    Here is how to fetch the file attachments:

    const ws = require('ws.js')
    const { Http, Mtom } = ws
    
    var handlers =  [ new Mtom(), new Http()];
    var request = '' +
                    '' +
                      '' +
                          '' +
                      '' +
                    '' +
                  ''
    
    var ctx = { request: request
              , contentType: "application/soap+xml"
              , url: "http://localhost:7171/Service/mtom"
              , action: "http://tempuri.org/IService/EchoFiles"
              }
    
    ws.send(handlers, ctx, function(ctx) {
      //read an attachment from the soap response
      var file = ws.getAttachment(ctx, "response", "//*[local-name(.)='File1']")
      // work with the file
      fs.writeFileSync("result.jpg", file)
    })
    

    Two limitations:

    • No basic auth provided out-of-box, patch required https://github.com/yaronn/ws.js/pull/40
    • If the file name is an url, you need to apply another patch at mtom.js. Replace:

    .

    xpath = "//*[@href='cid:" + encodeURIComponent(id) + "']//parent::*"
    

    with:

    xpath = "//*[@href='cid:" + id + "']//parent::*"
    

提交回复
热议问题