【问题标题】:How to add file attachment to soap request with node-soap library ?如何使用节点肥皂库向肥皂请求添加文件附件?
【发布时间】:2018-02-02 09:58:41
【问题描述】:

我需要将文件附件添加到来自 node.js 应用程序的 soap 请求。

我可以使用 node-soap 库发送请求,现在我需要在请求中添加一个文件。

我是用 java 客户端或soapUI 做的,但我必须在 node.js 中做,也许有可能做那个覆盖默认请求对象?

【问题讨论】:

    标签: node.js soap-client node-soap


    【解决方案1】:

    我没有找到使用 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) {
            ...
        }
    }
    

    【讨论】:

    • 我有一个 SOAP 服务器,希望在某个输入点附加附件,我该如何处理?因为我在您的示例中没有看到对您的附件的任何引用
    【解决方案2】:

    我找到了一种使用 base64 编码的 node-soap 发送附件的方法 这是一个例子

    import soap from 'soap'
    import fs from 'fs'
    
    async function main(){
      const filepath = '/path/to/attachment/file.extension'
      const client = await soap.createClientAsync(/* ... */)
      const result = await client.AnApiMethodAsync({
        expectedKeyForTheFile: await fs.readFileAsync(filepath, 'base64')
      })
    }
    
    main()
    

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多