Skip to main content
1-Visitor
June 15, 2017
Solved

How to consume SOAP Web service in thingworx?

  • June 15, 2017
  • 5 replies
  • 7883 views

I want to call SOAP web service from thingworx which is wrote in JAVA. I use getXML snippet for same. But I can not understand prameters we need to pass in snippet. Please suggest solution fotr this. Also we need to send some parameters for calling web service. How can we pass parameters using getXML?

Best answer by CarlesColl

Here you have a sample of my last SOAP consume implementation:

var content ='<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsd="https://mydomain.com/wsdl">\

    <x:Header/>\

    <x:Body>\

        <wsd:remoteSoapServiceName/>\

    </x:Body>\

   </x:Envelope>';

var resultXML =

  Resources["ContentLoaderFunctions"].PostXML({

  proxyScheme: undefined /* STRING */,

  headers: undefined /* JSON */,

  ignoreSSLErrors: undefined /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: undefined /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: "https://mydomain.com/service/wsfia/compub" /* STRING */,

  content: content,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: undefined /* STRING */,

  domain: undefined /* STRING */,

  username: undefined /* STRING */

});

var returnXMLString = resultXML.*::Body.*::remoteSoapServiceNameResponse.return;

var str = ""+returnXMLString[0].toString().replace(/\n/gi,"");

......

5 replies

1-Visitor
June 16, 2017

Here you have a sample of my last SOAP consume implementation:

var content ='<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsd="https://mydomain.com/wsdl">\

    <x:Header/>\

    <x:Body>\

        <wsd:remoteSoapServiceName/>\

    </x:Body>\

   </x:Envelope>';

var resultXML =

  Resources["ContentLoaderFunctions"].PostXML({

  proxyScheme: undefined /* STRING */,

  headers: undefined /* JSON */,

  ignoreSSLErrors: undefined /* BOOLEAN */,

  useNTLM: undefined /* BOOLEAN */,

  workstation: undefined /* STRING */,

  useProxy: undefined /* BOOLEAN */,

  withCookies: undefined /* BOOLEAN */,

  proxyHost: undefined /* STRING */,

  url: "https://mydomain.com/service/wsfia/compub" /* STRING */,

  content: content,

  timeout: undefined /* NUMBER */,

  proxyPort: undefined /* INTEGER */,

  password: undefined /* STRING */,

  domain: undefined /* STRING */,

  username: undefined /* STRING */

});

var returnXMLString = resultXML.*::Body.*::remoteSoapServiceNameResponse.return;

var str = ""+returnXMLString[0].toString().replace(/\n/gi,"");

......

rsalunkhe1-VisitorAuthor
1-Visitor
June 20, 2017

Hi Carles,

I followed your code but doesn't helpful for me. Here is my code

var content = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hs="http://www.holidaywebservice.com/HolidayService_v2/">\
<soapenv:Body>\
<hs:GetHolidaysAvailable>\
<hs:countryCode>UnitedStates</hs:countryCode>\
</hs:GetHolidaysAvailable>\
</soapenv:Body>\
</soapenv:Envelope>';


var params = {
proxyScheme: undefined /* STRING */,
headers: undefined /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: "http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl" /* STRING */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
username: undefined /* STRING */
};

// result: XML
var result = Resources["ContentLoaderFunctions"].GetXML(params);

rsalunkhe1-VisitorAuthor
1-Visitor
June 20, 2017

My updated code is below: following error occurs: I Parse XML namespaces as per https://support.ptc.com/appserver/cs/view/solution.jsp?n=CS219642&lang=en&source=snippet

Unable To Convert From org.mozilla.javascript.xmlimpl.XMLList to XML

var content = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hs="http://www.holidaywebservice.com/HolidayService_v2/">\
<soapenv:Body>\
<hs:GetHolidaysAvailable>\
<hs:countryCode>UnitedStates</hs:countryCode>\
</hs:GetHolidaysAvailable>\
</soapenv:Body>\
</soapenv:Envelope>';

var params = {
proxyScheme: undefined /* STRING */,
headers: undefined /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: "http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl" /* STRING */,
content: undefined /* XML */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
username: undefined /* STRING */
};

// result: XML
var xmlPage = Resources["ContentLoaderFunctions"].GetXML(params);


var result = xmlPage.*::soap.GetHolidaysAvailableResponse.GetHolidaysAvailableResult

Here is <soap:envelope> response from Postman:

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <soap:Body>

        <GetHolidaysAvailableResponse xmlns="http://www.holidaywebservice.com/HolidayService_v2/">

            <GetHolidaysAvailableResult>

                <HolidayCode>

                    <Code>NEW-YEARS-DAY-ACTUAL</Code>

                    <Description>New Year's Day</Description>

                </HolidayCode>

                <HolidayCode>

                    <Code>NEW-YEARS-DAY-OBSERVED</Code>

                    <Description>New Year's Day</Description>

                </HolidayCode>

                <HolidayCode>

                    <Code>MARTIN-LUTHER-KING-BIRTHDAY-ACTUAL</Code>

                    <Description>Martin Luther King's Birthday</Description>

                </HolidayCode>

                <HolidayCode>

                    <Code>MARTIN-LUTHER-KING-BIRTHDAY-OBSERVED</Code>

                    <Description>Martin Luther King's Birthday</Description>

                </HolidayCode>

                <HolidayCode>

                    <Code>GROUNDHOG-DAY</Code>

                    <Description>Groundhog Day</Description>

             

                <HolidayCode>

                    <Code>ASH-WEDNESDAY</Code>

                    <Description>Ash Wednesday</Description>

                </HolidayCode>

                <HolidayCode>

                    <Code>VETERANS-DAY-OBSERVED</Code>

                    <Description>Veteran's Day</Description>

                </HolidayCode>

              

                <HolidayCode>

                    <Code>NEW-YEARS-EVE</Code>

                    <Description>New Year's Eve</Description>

                </HolidayCode>

            </GetHolidaysAvailableResult>

        </GetHolidaysAvailableResponse>

    </soap:Body>

</soap:Envelope>

1-Visitor
June 21, 2017

You should be referencing XML nodes wrongly, I'm not super used to it, and if you aren't, you should test variations on how to reference it, and/or understand well.

Try to recover first a lower level on the XML hierarchy for instance.

On my case once I got the answer, I had to convert to string, remove new lines and convert back again to XML ( with a in memory property of type XML ).

Carles.

rsalunkhe1-VisitorAuthor
1-Visitor
June 22, 2017

Hi Carles Coll​,

Your support is valuable for me.

Your code is very helpful. But data comes with xml tag. Following is my code. I created SOAP_DataShape and create infotable from that datashape. In for each I parse xml according to tags. You can see xml directly hitting url in browser.


var params = {

 

proxyScheme: undefined /* STRING */,

 

headers: undefined /* JSON */,

 

ignoreSSLErrors: undefined /* BOOLEAN */,

 

useNTLM: undefined /* BOOLEAN */,

 

workstation: undefined /* STRING */,

 

useProxy: undefined /* BOOLEAN */,

 

withCookies: undefined /* BOOLEAN */,

 

proxyHost: undefined /* STRING */,

 

url: "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx/GetCountriesAvailable" /* STRING */,

 

timeout: undefined /* NUMBER */,

 

proxyPort: undefined /* INTEGER */,

 

password: undefined /* STRING */,

 

domain: undefined /* STRING */,

 

username: undefined /* STRING */

 

};

 

// result: XML

 

var resultXML = Resources["ContentLoaderFunctions"].GetXML(params);

 

 

var params = {

 

  infoTableName : "InfoTable",

 

  dataShapeName : "SOAP_DataShape"

 

};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(StringDataShape)

 

var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params);

for each (var tag in resultXML.*::CountryCode.*::Code) {

 

    var newRow = new Object();

 

    newRow.HolidayCode = tag;

 

    newRow.Description=tag;

 

    result.AddRow(newRow);

 

}

Here is URL: http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx/GetCountriesAvailable

I want only value extracted from tag. What should I do for it??

Here is my output. Please add your comments. And you

 


Your reply is valuable for me.

 

 


1-Visitor
June 22, 2017

Fast solution : tag.replace('<Code xmlns=......","").replace("</Code>","");

rsalunkhe1-VisitorAuthor
1-Visitor
June 22, 2017

Carles Coll

It gives following error

TypeError: INVALID_CHARACTER_ERR: An invalid or illegal XML character is specified. (Demo_SOAP#31)

5-Regular Member
June 22, 2017
rsalunkhe1-VisitorAuthor
1-Visitor
June 28, 2017

Hi Carles Coll​,

My first call of SOAP is successful but bad luck for second call. can you check my code? It returns empty response. It works fine in Postman. But not work in TW. Here I pass "United States" as parameter to "countryCode"

Note: I am using wsdl file here.

var content = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:hs="http://www.holidaywebservice.com/HolidayService_v2/">\
<soapenv:Body>\
<hs:GetHolidaysAvailable>\
<hs:countryCode>UnitedStates</hs:countryCode>\
</hs:GetHolidaysAvailable>\
</soapenv:Body>\
</soapenv:Envelope>';


var params1 = {
proxyScheme: undefined /* STRING */,
headers: undefined /* JSON */,
ignoreSSLErrors: undefined /* BOOLEAN */,
useNTLM: undefined /* BOOLEAN */,
workstation: undefined /* STRING */,
useProxy: undefined /* BOOLEAN */,
withCookies: undefined /* BOOLEAN */,
proxyHost: undefined /* STRING */,
url: "http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl" /* STRING */,
timeout: undefined /* NUMBER */,
proxyPort: undefined /* INTEGER */,
password: undefined /* STRING */,
domain: undefined /* STRING */,
username: undefined /* STRING */
};

// result: XML
var resultXML = Resources["ContentLoaderFunctions"].GetXML(params1);

var params2 = {
  infoTableName : "InfoTable",
  dataShapeName : "SOAP_DataShape"
};

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(StringDataShape)
var result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(params2);

for each (var tag in resultXML.*::HolidayCode) {
    var newRow = new Object();
    newRow.HolidayCode = tag.*::Code;
    newRow.Description=tag.*::Description;
    result.AddRow(newRow);
}