node.js - 如何使用 node-soap 库将文件附件添加到 soap 请求?

标签 node.js soap-client node-soap

我需要向来自 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/

相关文章:

node.js - 在 Node.js 中管理 session ?

node.js - NodeJS - 运行 shell 命令,退出,转移到 shell 命令

java - JAVA 中的 SOAP 读取器和编写器?

node.js - 是否可以从 NodeJS 访问 Microsoft Dynamics NAV Web 服务?

javascript - Sequelize JS : How to remove nested object in result set

node.js - 抓取谷歌翻译

node.js - 使用 Strong-Soap 通过 Node 进行简单 SOAP 调用

xml - 尝试通过 Node 调用国家铁路 SOAP API 时出现类型错误

c# - 如何删除 <a :To s:mustUnderstand ="1"> from SOAP Request

php - SoapClient 调用返回 500 Internal Server Error