Rahul Saini Rahul Saini - 1 year ago 82
Node.js Question

How to integrate soap xml API of unicommerce in nodejs

I am trying to hit xml Api of unicommerce by soap module in nodejs. It is working fine in soapUI.But is not integrated in node Application.
I am getting this error.


ERROR: {Error: Cannot parse response}.


var express = require("express");
var soap = require("soap");
var http = require('http');
var app = express();
var router = express.Router();
var username = "*********";
var password = "************";
var auth = "Basic " + new Buffer(username + ":" + password).toString("base64");// not working

var wsdlUrl = 'https://lensclues.unicommerce.com/services/soap/uniware16.wsdl?facility=01';

const Autentication = '<soapenv:Header>' +
'<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">' +
'<wsse:UsernameToken wsu:Id="UsernameToken-D6BE484999DA5E97D4148483888689316">' +
'<wsse:Username>**********</wsse:Username>' +
'<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*******************</wsse:Password>'+
'<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">$$$$$$$$$$$</wsse:Nonce>'+
'<wsu:Created>2017-01-19T15:14:46.893Z</wsu:Created>'+
'</wsse:UsernameToken>'+
'</wsse:Security>' +
'</soapenv:Header>'
soap.createClient(wsdlUrl, function (err, soapClient) {
soapClient.addSoapHeader(Autentication);
const data = '<soapenv:Envelope xmlns:ser="http://uniware.unicommerce.com /services/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' + '<soapenv:Body>' +
'<ser:GetInventorySnapshotRequest>' +
'<ser:ItemTypes>' +
'<ser:ItemType>' +
'<ser:ItemSKU>LCSGDB555xD15165S4PURx124</ser:ItemSKU>' +
'</ser:ItemType>' +
'</ser:ItemTypes>' +
'<ser:UpdatedSinceInMinutes>600</ser:UpdatedSinceInMinutes>' +
'</ser:GetInventorySnapshotRequest>' +
'</soapenv:Body>' +
'</soapenv:Envelope>'
if (err) {
console.log("err", err);
}
soapClient.GetInventorySnapshot({
data
}, function (err, result) {
if (err) {
console.log("ERROR:", err);
}
else {
console.log("result", result);
}
});


The question is: How do I send the request and print the answer?
Could you have any clues about this kind of issue?

Thanks a lot!

Answer Source

Try this send data xml format

 xw = new XMLWriter;
            xw.startDocument();
            xw.startElement('soapenv:Envelope');
            xw.writeAttribute('xmlns:ser', 'http://uniware.unicommerce.com/services/');
            xw.writeAttribute('xmlns:soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
            xw.startElement('soapenv:Header');
            xw.startElement('wsse:Security');
            xw.writeAttribute('soapenv:mustUnderstand', '1');
            xw.writeAttribute('xmlns:wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd');
            xw.startElement('wsse:UsernameToken');
            xw.startElement('wsse:Username');
            xw.text('lensclue');
            xw.endElement('wsse:Username');
            xw.startElement('wsse:Password');
            xw.text('5a996796-1353-4141-8ec4-0a1be02b8c75');
            xw.endElement('wsse:Password');
            xw.endElement('wsse:UsernameToken');
            xw.endElement('wsse:Security');
            xw.endElement('soapenv:Header');
            xw.startElement('soapenv:Body');
            xw.startElement('ser:GetInventorySnapshotRequest');
            xw.startElement('ser:ItemTypes');
            xw.startElement('ser:ItemType');
            xw.startElement('ser:ItemSKU');
            xw.text(sku);
            xw.endDocument();
            /*************************Request Module******************/
            request(
                {
                    'method': "POST",
                    'url': 'https://lensclues.unicommerce.com/services/soap/uniware16.wsdl?',
                    'body': xw.toString(),
                    'Content-Type': 'text/xml'
                },
                function (error, response, body) {
                    if (!error && response.statusCode == 200) {
                        parseString(body, function (err, result) {
                            let tt = result['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetInventorySnapshotResponse'][0]['Successful'][0]
                            console.log("Error---------->", tt);
                            console.log("Result---------->", result);
                            if (tt == "false") {
                                var check = result['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetInventorySnapshotResponse'][0]['Errors'][0]['Error'][0];
                                console.log("check", check);
                                mainCallback(check, err);
                            }
                            else {
                                let inventoryValue = result['SOAP-ENV:Envelope']['SOAP-ENV:Body'][0]['GetInventorySnapshotResponse'][0]['InventorySnapshots'][0]["InventorySnapshot"][0]['Inventory'][0];
                                console.log("inventoryValue", inventoryValue);
                                if (inventoryValue >= prodQty) {
                                    mainCallback(null, inventoryValue);
                                }
                                else {
                                    mainCallback("Inventory not be available", err);
                                }

                            }
                        });
                    }
                    else {
                        console.log("------------->", error)
                        mainCallback("error in to fetch inventory data", error);
                    }
                }
            );
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download