Rangarajan K
Rangarajan K

Reputation: 83

Namespace coming as undefined in the response body for soap in node js.

I am using node js with the soap module 'node-soap' version 0.24.0 (latest).

I am implementing a api framework with the following wsdl file, server.js file. For organisational reasons the wsdl operation is just like a sample template similar to structure of original code.When I make a SOAP request, its being processed successfully but in the response , namespace part in the tags is coming as undefined. This is causing problems to our client who will be making the api calls, they are not able to read the response properly.

I am unable to find a fix for this and do not know if I shoukld change something in the wsdl file or the server.js.

wscalc1.wsdl

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="wscalc1" targetNamespace="http://localhost:8000/wscalc1"
                  xmlns="http://localhost:8000/wscalc1"
                  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <wsdl:message name="multiplicarRequest">
    <wsdl:part name="a" type="xs:string"/>
    <wsdl:part name="b" type="xs:string"/>
  </wsdl:message>
  <wsdl:message name="multiplicarResponse">
    <wsdl:part name="mulres" type="xs:string"/>
  </wsdl:message>
  <wsdl:portType name="calcP">
    <wsdl:operation name="multiplicar">
      <wsdl:input message="multiplicarRequest"/>
      <wsdl:output message="multiplicarResponse" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="calcB" type="calcP">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="multiplicar">
      <soap:operation soapAction="multiplicar"/>
      <wsdl:input>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ws">
    <wsdl:port binding="calcB" name="calc">
      <soap:address location="http://localhost:8001/wscalc1"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

server.js

"use strict";

var soap = require('soap');
var http = require('http');

var service = {
    ws: {
        calc: {
            multiplicar : function(args) {
                var n = args.a * args.b;
                return { mulres : n };
            }
        }
    }
};

var xml = require('fs').readFileSync('wscalc1.wsdl', 'utf8');

var server = http.createServer(function(request,response) {
    response.end("404: Not Found: "+request.url);
});

server.listen(8001);
soap.listen(server, '/wscalc1', service, xml);

post request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsc="http://localhost:8000/wscalc1">
   <soapenv:Header/>
   <soapenv:Body>
      <wsc:multiplicar>
         <a>2</a>
         <b>3</b>
      </wsc:multiplicar>
   </soapenv:Body>
</soapenv:Envelope>

response

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" > 
    <soap:Body>
        <undefined:multiplicarResponse>
            <undefined:mulres>6</undefined:mulres>
        </undefined:multiplicarResponse>
    </soap:Body>
</soap:Envelope>

Upvotes: 0

Views: 2180

Answers (2)

czuniga
czuniga

Reputation: 139

I have been able to make it work, but I had to do some changes to the wsdl, I also used Eclipse wsdl editor to verify if there are any error. Please find the details below:

wscalc1.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:8000/wscalc1" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="wscalc1" targetNamespace="http://localhost:8000/wscalc1">
<wsdl:message name="sumarRequest">
    <wsdl:part name="a" type="xsd:string"></wsdl:part>
    <wsdl:part name="b" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="sumarResponse">
  <wsdl:part name="sumres" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="multiplicarRequest">
  <wsdl:part name="a" type="xsd:string"></wsdl:part>
  <wsdl:part name="b" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:message name="multiplicarResponse">
  <wsdl:part name="mulres" type="xsd:string"></wsdl:part>
</wsdl:message>
<wsdl:portType name="calcP">
  <wsdl:operation name="sumar">
        <wsdl:input message="tns:sumarRequest"></wsdl:input>
        <wsdl:output message="tns:sumarResponse"/>
  </wsdl:operation>
  <wsdl:operation name="multiplicar">
    <wsdl:input message="tns:multiplicarRequest"></wsdl:input>
    <wsdl:output message="tns:multiplicarResponse"></wsdl:output>
  </wsdl:operation>
</wsdl:portType>
<wsdl:binding name="calcB" type="tns:calcP">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
  <wsdl:operation name="sumar">
    <soap:operation soapAction="sumar" style="rpc"/>
    <wsdl:input>
      <soap:body namespace="http://localhost:8000/wscalc1" use="literal"/>
    </wsdl:input>
    <wsdl:output>
      <soap:body namespace="http://localhost:8000/wscalc1" use="literal"/>
    </wsdl:output>
  </wsdl:operation>
  <wsdl:operation name="multiplicar">
    <soap:operation soapAction="multiplicar" style="rpc"/>
    <wsdl:input>
      <soap:body namespace="http://localhost:8000/wscalc1" use="literal"/>
    </wsdl:input>
    <wsdl:output>
      <soap:body namespace="http://localhost:8000/wscalc1" use="literal"/>
    </wsdl:output>
  </wsdl:operation>
</wsdl:binding>
<wsdl:service name="ws">
<wsdl:port binding="tns:calcB" name="calc">
  <soap:address location="http://localhost:8001/wscalc1"/>
</wsdl:port>
  </wsdl:service>
</wsdl:definitions>

server.js

const soap = require('soap');
const http = require('http');

const service = {
  ws: {
    calc: {
       sumar : function(args) {
          var n = 1*args.a + 1*args.b;
          return { sumres : n };
       },
       multiplicar : function(args) {
          var n = args.a * args.b;
          return { mulres : n };
       }
     }
   }
};

const xml = require('fs').readFileSync('wscalc1.wsdl', 'utf8');

const server = http.createServer(function(request,response) {
   response.end("404: Not Found: "+request.url);
});

server.listen(8001);
soap.listen(server, '/wscalc1', service, xml);

soap request

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsc="http://localhost:8000/wscalc1">
  <soapenv:Header/>
  <soapenv:Body>
     <wsc:sumar>
        <a>2</a>
        <b>3</b>
     </wsc:sumar>
  </soapenv:Body>
</soapenv:Envelope>

response

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://localhost:8000/wscalc1">
  <soap:Body>
     <tns:sumarResponse>
        <tns:sumres>5</tns:sumres>
     </tns:sumarResponse>
  </soap:Body>
</soap:Envelope>

Upvotes: 1

Nikita Petrov
Nikita Petrov

Reputation: 59

I had the same issue. I'm not sure how to apply my solution to your code, but the idea is that you need to specify your namespace in <definitions> tag and assign a prefix for it, like:

<definitions
    name="ININWebService"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="WebServices.TVTraffic"
    targetNamespace="WebServices.TVTraffic"
 >

Then I used the same prefix 'tns' to define my messages and elements, like element="tns:updatePayment" instead of element="updatePayment" and so on, and finally I've got working namespace prefixes.

Hope it helps.

Upvotes: 1

Related Questions