Unable to handle soap multipart response in node.js

Viewed 228

All i need to do is make a soap service with Node to consume another soap service and return the same response that this service returns. All i was doing at first was setting the body and headers that i got when i consume the service to the new response but i have a problem because the service returns a file and the response of the node is breaking the base64.

expressSoap.js

const express = require('express');
const bodyParser = require('body-parser');
const appSoap = express();
const routes = require('../routes/digitalSignature.route');

appSoap.use(bodyParser.text({ type: '*/*' }));
appSoap.use('/dsServices', routes);

module.exports = appSoap;

routes.js

const asyncHandler = require('express-async-handler');
const dsConnector = require('../thuban-connector/ds.connector');
const express = require('express');
const router = express.Router(); // eslint-disable-line new-cap

router.post('/', asyncHandler(dsService));


async function dsService(req, res){
    const url = 'url';
    var request = require('request');

    var options = {
       url: 'url',
       method: 'POST',
       body: req.body,
       headers: req.headers
    };


    let callback = (error, response, body) => {

        if (!error && response.statusCode == 200) {
           console.log(response);
           res.header(response.headers);
           res.status(response.statusCode);
           res.write(body);
           res.end();
       };
    };

    request(options, callback); 
}

Part of the response i am returning from the node:

HTTP/1.1 200 OK
X-Powered-By: Express
accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
soapaction: ""
content-type: Multipart/Related; boundary="----=_Part_281_261916788.1605890673731"; type="application/xop+xml"; start-info="text/xml"
transfer-encoding: chunked
date: Fri, 20 Nov 2020 16:44:33 GMT
keep-alive: timeout=20
connection: keep-alive

------=_Part_281_261916788.1605890673731
Content-Type: application/xop+xml; charset=utf-8; type="text/xml"

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> 
...
<ns2:documentData><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:0ca4170e-b4f0-49e1-be57-422911091e4f%40vivatia.com"/></ns2:documentData>
...
</SOAP-ENV:Envelope>
------=_Part_281_261916788.1605890673731
Content-Type: */*
Content-ID: <0ca4170e-b4f0-49e1-be57-422911091e4f@vivatia.com>
Content-Transfer-Encoding: binary

%PDF-1.4
%����
1 0 obj
<<
/Creator (Apache FOP Version 2.3)
/Producer (Apache FOP Version 2.3)
/CreationDate (D:20201019225537+02'00')
>>
endobj
2 0 obj
<<
  /N 3
  /Length 3 0 R
  /Filter /FlateDecode
>>
stream
x���wP�������^�ewY:,�I�����Dz�]:+,��`"��4% ����HQ,���,��"*(Y��Ifޛ��ͼ��~�3��s��@(N�$��
...

Part of the response as it should be:

HTTP/1.1 200 
Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
SOAPAction: ""
Content-Type: Multipart/Related; boundary="----=_Part_282_2075935262.1605893289941"; type="application/xop+xml"; start-info="text/xml"
Transfer-Encoding: chunked
Date: Fri, 20 Nov 2020 17:28:09 GMT
Keep-Alive: timeout=20
Connection: keep-alive

------=_Part_282_2075935262.1605893289941
Content-Type: application/xop+xml; charset=utf-8; type="text/xml"

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
...
<ns2:documentData><xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:99cdc8c9-4759-4ad9-84c0-95eb84adf0a2%40vivatia.com"/></ns2:documentData>
..
</SOAP-ENV:Envelope>
------=_Part_282_2075935262.1605893289941
Content-Type: */*
Content-ID: <99cdc8c9-4759-4ad9-84c0-95eb84adf0a2@vivatia.com>
Content-Transfer-Encoding: binary

%PDF-1.4
%ª«¬­
1 0 obj
<<
/Creator (Apache FOP Version 2.3)
/Producer (Apache FOP Version 2.3)
/CreationDate (D:20201019225537+02'00')
>>
endobj
2 0 obj
<<
  /N 3
  /Length 3 0 R
  /Filter /FlateDecode
>>
stream
xœ­–wPÔ×Çïï÷Û^ØewY:,½I•²€ôÞDzØ]:+,½Ø�`"ˆˆ4% ¢€£¡HQ,ˆ‚ö,”Œ"*(Yô½IfÞ›÷æÍ¼ï÷~æœ3÷œsÏ÷@(Nà$¦À
...

Crearly its a problem related to encoding but i am not able to fix it. Even i cant see in node (console, file,etc) the response (when i call the other service) like it should be :

stream
xœ­–wPÔ×Çïï÷Û^ØewY:,½I•²€ôÞDzØ]:+,½Ø�`"ˆˆ4% ¢€£¡HQ,ˆ‚ö,”Œ"*(Yô½IfÞ›÷æÍ¼ï÷~æœ3÷œsÏ÷@(Nà$¦À
...
0 Answers
Related