1

I am writing a Node JS SOAP client using Node-Soap module to send a file to a remote SharePoint based Web Services.

The machine client requires a proxy to access Internet and the SharePoint WS requires an account (user, pwd). Below are the code source.

However, I always have the error "(node:20857) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Cannot parse response".

Someone can help me, please?

var process = require('process');
var fs = require('fs');
var request = require('request')
var soap = require('soap');
var apiWSDL = '.../test-collab/WS/_vti_bin/copy.asmx?wsdl';


function sendFile() {

var p = new Promise(function (resolve, reject) {

    request_with_defaults = request.defaults({
        'proxy': 'http://***:***@10.115.108.109:8080',
        'timeout': 50000,
        'connection': 'keep-alive'
    });


    var options = {
        'request': request_with_defaults,
        endpoint: 'https://.../test-collab/WS/_vti_bin/copy.asmx',
    }

    var byteArray = fs.readFileSync('test.txt').toString('base64');

    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    //process.env.https_proxy = 'http://***@***:10.115.108.109:8080';

    soap.createClient(apiWSDL, options, function (err, client) {
        if (err) throw new Error(err);

        var args = {
            DestinationUrls: 'https://.../test-collab/WS/CAS/test.txt',
            Stream: byteArray
        }

        client.setSecurity(new soap.ClientSSLSecurity(null, null, null, {   /*default request options like */
            strictSSL: false,
            rejectUnauthorized: false,
            // hostname: 'some-hostname'
            //secureOptions: constants.SSL_OP_NO_TLSv1_2,
            forever: true,
        }));

        client.addHttpHeader('vm6_webapp', 'SERVICE');
        client.addHttpHeader('vm6_password', '***');
        client.addHttpHeader('vm6_user', '***');
        client.CopyIntoItems(args, function (err, result) {
            //console.log(err);
            if (err) {
                console.log(err);
                reject(err);
            }
            var sets;
            try {
                console.log(result);
                if (result.length) {
                    resolve(result);
                } else {
                    reject(result);
                }
            } catch (error) {
                console.log("error");
                reject("error und")
            }



        });
    });


});

return p;

}

2 Answers2

0

As the error message is Cannot parse response, two possibilities can happen:

  • either the response is not in XML format
  • or the XML response is not acceptable by SOAP response message defined in wsdl.

Can you redo the SOAP request by using an existing client, such as, SoapUI to confirm?

Otherwise, I propose to use console.error( err.stack ) rather than console.log( err ) to get the full execution trace of err.

nhnghia
  • 725
  • 9
  • 8
0

Thank Nghia for your reply.

In fact, I've written a SOAP client in Java for this WebServers before and it works. That means the paramters are ok as well as the XML response is ok.

Here are the code in Java:

 MoccaClient clientWSMocca = new MoccaClient();
        CopySoap copySoap = (CopySoap)clientWSMocca.getClient(site_soap_url,
                proxy_host, proxy_port, proxy_user, proxy_password,
                mocca_user, mocca_password, mocca_web_app,
                CopySoap.class);

        // Récupération sous forme de tableau de bytes du fichier
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {

            InputStream in = new BufferedInputStream(new FileInputStream(file_path));

            BufferedOutputStream bufOut = new BufferedOutputStream(out);
            for (int b = in.read(); b != -1; b = in.read()) {
                bufOut.write(b);
            }
            in.close();
            bufOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Initialisation des variables de contexte
        FieldInformation fieldInformation = new FieldInformation();

        String destinationUrl = site_url + file_name;

        DestinationUrlCollection destinationUrlCollection = new DestinationUrlCollection();
        destinationUrlCollection.getString().add(destinationUrl);
        FieldInformationCollection fieldInformationCollection = new FieldInformationCollection();
        fieldInformationCollection.getFieldInformation().add(fieldInformation);
        Holder<CopyResultCollection> copyResult= new Holder<CopyResultCollection>();
        Holder<Long> getItemResult = new Holder<Long>();

        copySoap.copyIntoItems(file_path, destinationUrlCollection, fieldInformationCollection, out.toByteArray(), getItemResult, copyResult);

MoccaClient.java

public class MoccaClient {
    public Object getClient(String url,
                            String proxyhost, int proxyport, String userproxy, String passwordproxy,
                            String moccauser, String moccapassword, String moccawebapp,
                            Class<?> serviceclass) {
        System.setProperty("org.apache.cxf.JDKBugHacks.defaultUsesCaches", "true");
        boolean bssl = false;
        if (url.startsWith("https")) {
            bssl = true;
        }

        if (url.startsWith("HTTPS")) {
            bssl = true;
        }

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.getInInterceptors().add(new MyInterceptor());
        factory.setServiceClass(serviceclass);
        factory.setAddress(url);
        Object client = factory.create();
        Client clientDuProxy = ClientProxy.getClient(client);
        Map<String, List<String>> headers = new HashMap();
        headers.put("vm6_user", Arrays.asList(moccauser));
        headers.put("vm6_password", Arrays.asList(moccapassword));
        headers.put("vm6_webapp", Arrays.asList(moccawebapp));
        clientDuProxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
        HTTPConduit http = (HTTPConduit)clientDuProxy.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        http.setClient(httpClientPolicy);
        if (proxyhost != null) {
            http.getClient().setProxyServer(proxyhost);
            http.getClient().setProxyServerPort(proxyport);
        }

        if (userproxy != null) {
            http.getProxyAuthorization().setUserName(userproxy);
            http.getProxyAuthorization().setPassword(passwordproxy);
        }

        if (bssl) {
            TrustManager[] trustCerts = new TrustManager[]{new AllTrust()};
            TLSClientParameters tcp = new TLSClientParameters();
            tcp.setTrustManagers(trustCerts);
            tcp.setSecureSocketProtocol("TLS");
            tcp.setDisableCNCheck(true);
            http.setTlsClientParameters(tcp);
        }

        return client;
    }
}