Consuming webservices in Coldfusion

On October 20, 2007, in ColdFusion, by Anuj Gakhar

We all know how to consume webservices in CF, using the CFINVOKE or using CreateObject(). However, using the above, its relatively hard to consume complex webservices, specially with complex datatypes. I normally use CFHTTP to the rescue which eliminates the need for any datatype mappings.

Here is an example. Lets try to consume the dateservices webservice available at http://webservices.daelab.net/datesservice/datesservice.wso?WSDL .

First of all, we need to identify the method we need to call and prepare its SOAP request manually. There are several ways of generating SOAP request including XML editors like XMLSpy etc. However, there are some SOAP services online that do this as well. Here is one, www.soapclient.com.

In this case, lets consume the DayName method of the service. The Request XML for that looks like this -. Sample SOAP request

Now, we need to submit this to the endpoint using CFHTTP. Here is how we do it.

<cfset sURL='http://webservices.daelab.net/datesservice
/datesservice.wso' />
<cfhttp url='#sURL#' method=&quot;post&quot; >
<cfheader name=&quot;Content-Type&quot; value=&quot;text/xml;&quot; charset=&quot;utf-8&quot;/>
<cfhttpparam type=&quot;xml&quot; value=&quot;#trim(requestXML)#&quot;/>
<cfhttpparam type=&quot;header&quot; name=&quot;SOAPAction&quot; value=&quot;DayName&quot;/>
</cfhttp&gt;

The output of that looks like this.

<cfdump var=&quot;#XmlParse(cfhttp.filecontent)#&quot; />

Next step would be parse the response XML to get the desired values and can be done in several different way, ofcourse.

Tagged with:  

2 Responses to Consuming webservices in Coldfusion

  1. Ted Johnson says:

    I’m trying to solve an error using cfinvoke. I get “Content is not allowed in prolog” apparently due to junk in front of the XML being returned.

    When i use your code, I’m getting “Variable REQUESTXML is undefined”.

    Any thoughts?

  2. Anuj Gakhar says:

    Hi Ted, you might want to read this
    http://www.bennadel.com/blog/1206-Content-Is-Not-Allowed-In-Prolog-ColdFusion-XML-And-The-Byte-Order-Mark-BOM-.htm

    And if you are using my code , did you copy the sample soap request and put it in a variable first ?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 445 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: