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.
[xml]
<cfset sURL=’http://webservices.daelab.net/datesservice
/datesservice.wso’ />
<cfhttp url=’#sURL#’ method="post" >
<cfheader name="Content-Type" value="text/xml;" charset="utf-8"/>
<cfhttpparam type="xml" value="#trim(requestXML)#"/>
<cfhttpparam type="header" name="SOAPAction" value="DayName"/>
</cfhttp>[/xml]
The output of that looks like this.
[xml]
<cfdump var="#XmlParse(cfhttp.filecontent)#" />[/xml]
Next step would be parse the response XML to get the desired values and can be done in several different way, ofcourse.
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?
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 ?