简体   繁体   中英

Cannot parse response SOAP Node.js

firstly sorry for my english, secondly I have a question about use of SOAP in Node.js. I am a beginner with node.js and I need help. This is my function:

var soap = require('soap');

var url = 'http://SOMETHING?wsdl';

var args = {
    accountId: 'xxxxx',
    userName: 'xxxxx',
    password: 'xxxxxx',
    targetNSAlias: 'tns',
    targetNamespace: 'http://api.ilient.com/'
};

soap.createClient(url, function(err, client) {
    if(err) throw err;    
            client.login(args,function(err, result, raw, soapHeader){
            if(err) throw err;
            console.log(result);
    });
});

when I run I get this error:

Error: Cannot parse response
at /root/node_modules/soap/lib/client.js:321:21
at Request._callback (/root/node_modules/soap/lib/http.js:117:5)
at Request.self.callback (/root/node_modules/request/request.js:186:22)
at Request.emit (events.js:98:17)
at Request.<anonymous> (/root/node_modules/request/request.js:1081:10)
at Request.emit (events.js:95:17)
at IncomingMessage.<anonymous> (/root/node_modules/request/request.js:1001:12)
at IncomingMessage.g (events.js:180:16)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:944:16

someone can help me solve it? Thanks and sorry for my english again.

I faced a similar problem. Maybe the SOAP web service that you are trying to consume has v1.2 specification and it might expect the content type as application/soap+xml instead of text/xml. In order to force node-soap to use SOAP 1.2 version you could add forceSoap12Headers: true among createClient() parameters.

On a side note, I had to add the ws-addressing headers to soap header because of The message with To ' ' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher error.

I edited your code as follows:

var soap = require('soap');
var url = 'http://SOMETHING?wsdl';
var args = {
    accountId: 'xxxxx',
    userName: 'xxxxx',
    password: 'xxxxxx',
    targetNSAlias: 'tns',
    targetNamespace: 'http://api.ilient.com/'
};

var soapOptions = {
    forceSoap12Headers: true
};

var soapHeaders = {
    'wsa:Action': 'http://tempuri.org/MyPortName/MyAction',
    'wsa:To': 'http://SOMETHING.svc'
};

soap.createClient(url, soapOptions, function(err, client) {
    if(err) throw err;  
    client.addSoapHeader(soapHeaders, '', 'wsa', 'http://www.w3.org/2005/08/addressing');  
    client.login(args, function(err, result, raw){
        if(err) throw err;
        console.log(result);
    });
});

Add this code: client.setSecurity(new soap.BasicAuthSecurity('username', 'password')); after your create client code. It worked for me:

var soap = require('soap');
var url = 'http://SOMETHING?wsdl';
soap.createClient(url, function(err, client) {
if(err) throw err;   
  client.setSecurity(new soap.BasicAuthSecurity('username', 'password'));
  client.login(args, function(err, result) {
  if(err) throw err;   
        console.log(result);
   });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM