Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add file attachment to soap request with node-soap library ?

I need to add an file attachment to a soap request from node.js application.

I am able to send request with node-soap library, and now I need to add a file to the request.

I did it with a java client or with soapUI, but I have to do it in node.js, maybe it's possible to do that overriding default request object ?

like image 876
Antoine Avatar asked Jan 28 '23 18:01

Antoine


1 Answers

I didn't find a solution with node-soap, I had to build manually my soap request :

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) {
        ...
    }
}
like image 61
Antoine Avatar answered Jan 31 '23 08:01

Antoine