我需要向来自 node.js 应用程序的 soap 请求添加文件附件。
我可以使用 node-soap 库发送请求,现在我需要向请求添加一个文件。
我是用 java 客户端或 soapUI 做的,但我必须在 node.js 中做,也许可以覆盖默认请求对象?
最佳答案
我没有找到 node-soap 的解决方案,我不得不手动构建我的 soap 请求:
var soapHeader = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header/><soapenv:Body><ws:method>';
var soapFooter = '</ws:method></soapenv:Body></soapenv:Envelope>';
function sendSoapRequestWithAttachments(soap,files){
var soapRequest = jsonToXml.buildObject(mail);
var finalSoapRequest = soapHeader + soapRequest + soapFooter;
var multipartMail = [];
// Add soap request
multipartMail.push({
'Content-Type': 'text/xml; charset=utf-8',
body: finalSoapRequest
});
// Add attachments
if (files) {
files.forEach(function (file) {
multipartMail.push({
'Content-Id': '<' + file.uuid + '>',
'Content-Type': 'application/octet-stream',
'Content-Transfer-Encoding': 'binary',
body: fs.createReadStream(file.path)
});
});
}
var options = {
uri: URL,
method: 'POST',
multipart: multipartMail
};
request.post(options, function (error, response) {
...
}
}
关于node.js - 如何使用 node-soap 库将文件附件添加到 soap 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48580124/