WYSIWYG

http://kufli.blogspot.com
http://github.com/karthik20522

Tuesday, March 31, 2015

Calling SOAP Service in Scala

Scala out of the box has limited capability to call SOAP service neither does libraries such as http-dispatch or spray client. SOAP service is reality is just a xml request/response service and lo and behold, XML is a first class citizen in Scala.

One of the widely used library is ScalaXB which helps in generating case classes given a xsd or wsdl file. Scalaxb is an XML data-binding tool for Scala that supports W3C XML Schema (xsd) and Web Services Description Language (wsdl) as the input file. This is great but it's quite hard to maintain and the code readability goes down the drain as the code is dynamically generated. For example, the following screen shot is what scalaxb generates when either a wsdl or xsd is provided


But what we really need is a trivial way to call a webservice using our existing http clients. Following is one way of doing so.
In this below example, i am calling a service that returns back a list of keywords given a list of keywordid's.
In the above script, all that i am doing is constructing the soap request headers manually and performing a POST operation. There are couple of things to be noted:
  • "SOAPAction" header is manually added to let the service know which service operation it is intended for
  • Setting the charset to UTF-8 and removing unicode characters "[^\\x20-\\x7e]"
  • Removing the unicode characters are necessary as scala fails to parse the response. This mostly seems to happens when calling .NET WCF services

GetKeywordDetailsRequest is a class that has the input parameters and has a function that generates the formatted xml for the soap request

Labels: , ,