如何在node.js中使用MTOM SOAP Web服务? [英] How to consume MTOM SOAP web service in node.js?
问题描述
我需要从node.js中基于soap的Web服务下载或处理文件. 有人可以建议我如何在node.js中处理此问题
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
我尝试使用"node-soap"或"soap" NPM模块.它适用于普通的肥皂网络服务.但是,不适用于二进制流或基于MTOM的SOAP Web服务
I tried with 'node-soap' or 'soap' NPM module. it worked for normal soap web service. But, not for binary steam or MTOM based SOAP web service
推荐答案
我想尝试回答这个问题……有趣的是2年零2个月后,我无法弄清楚如何轻松解决同一问题
I want to try to answer this... It's quite interesting that 2 years and 2 months later I can not figure it out how to easily solve the same problem.
我正在尝试从以下响应中获取附件:
I'm trying to get the attachment from a response like:
...
标题:{'cache-control':'no-cache ="set-cookie"', 'content-type':'multipart/related; boundary ="---- = _ Part_61_425861994.1525782562904"; type ="application/xop + xml"; start ="; start-info ="text/xml"',
headers: { 'cache-control': 'no-cache="set-cookie"', 'content-type': 'multipart/related;boundary="----=_Part_61_425861994.1525782562904";type="application/xop+xml";start="";start-info="text/xml"',
...
正文:'------ = _ Part_61_425861994.1525782562904 \ r \ n内容类型: application/xop + xml; charset = utf-8; type ="text/xml" \ r \ nContent-Transfer-Encoding:8bit \ r \ nContent-ID: \ r \ n \ r \ n .... \ r \ n ------ = _ Part_61_425861994.1525782562904 \ r \ n内容类型: application/octet-stream \ r \ nContent-Transfer-Encoding: 二进制\ r \ n内容ID: \ r \ n \ r \n PNG\ r \ n \ u001a \ n \ u0000 \ u0000 \ u0000 \ rIHDR \ u0000 \ u0000 \ u0002,\ u0000 \ u0000 \u0005 \ b \ u0006 \ u0 .... .... binary ....
body: '------=_Part_61_425861994.1525782562904\r\nContent-Type: application/xop+xml; charset=utf-8; type="text/xml"\r\nContent-Transfer-Encoding: 8bit\r\nContent-ID: \r\n\r\n....\r\n------=_Part_61_425861994.1525782562904\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\nContent-ID: \r\n\r\n�PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0002,\u0000\u0000\u0005�\b\u0006\u0........binary....
我尝试了 ws.js ,但没有解决方案.
I tried ws.js but no solution.
我的解决方案:
var request = require("request");
var bsplit = require('buffer-split')
// it will extract "----=_Part_61_425861994.1525782562904" from the response
function getBoundaryFromResponse(response) {
var contentType = response.headers['content-type']
if (contentType && contentType.indexOf('boundary=') != -1 ) {
return contentType.split(';')[1].replace('boundary=','').slice(1, -1)
}
return null
}
function splitBufferWithPattern(binaryData, boundary) {
var b = new Buffer(binaryData),
delim = new Buffer(boundary),
result = bsplit(b, delim);
return result
}
var options = {
method: 'POST',
url: 'http://bla.blabal.../file',
gzip: true,
headers: {
SOAPAction: 'downloadFile',
'Content-Type': 'text/xml;charset=UTF-8'
},
body: '<soapenv: ... xml request of the file ... elope>'
};
var data = [];
var buffer = null;
var filename = "test.png"
request(options, function (error, response, body) {
if (error) throw new Error(error);
if (filename && buffer) {
console.log("filename: " + filename)
console.log(buffer.toString('base64'))
// after this, we can save the file from base64 ...
}
})
.on('data', function (chunk) {
data.push(chunk)
})
.on('end', function () {
var onlyPayload = splitBufferWithPattern(Buffer.concat(data), '\r\n\r\n') // this will get from PNG
buffer = onlyPayload[2]
buffer = splitBufferWithPattern(buffer, '\r\n-')[0]
console.log('Downloaded.');
})
我不确定它在大多数情况下都可以使用.在我看来,它看起来像是不稳定的代码,因此我正在寻找更好的东西.
I am not sure it will works in most of the cases. It looks like unstable code to my eyes and so I'm looking for something better.
这篇关于如何在node.js中使用MTOM SOAP Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!