<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Creating and Consuming Web Service in ColdFusion

Published on
14,953 Points
5,253 Views
2 Endorsements
Last Modified:
Awarded
A web service is a software related technology that facilitates machine-to-machine interaction over a network.

This article helps beginners in creating and consuming a web service using the ColdFusion Markup Language (CFML) and associated application server.  The article will go through the following steps:

Create a ColdFusion component (CFC).
Create functions within a CFC.
Convert a function to web method to make the CFC a web service.
Publish / test the web service.
Consume the web service.

1. Create a ColdFusion component (CFC)


If you have been developing custom functions and ColdFusion components you are already well on your way to creating a web service.

ColdFusion components are almost OOP based but not totally; they have methods and constructs, and they provide reusable code.



If you know what functions achieve in any programming language, you are not far from understanding components. They turn functions into objects whose methods can be accessed.

The coldfuson component file should be in <cfcomponent>.
<cfcomponent>
      <cffunction name="myFunction"  returntype="string" output="no">
            <cfargument name="myArgument" type="string" required="yes">
            <cfset myResult="Welcome Srikanth">
            <cfreturn myResult>
      </cffunction>
</cfcomponent>

Open in new window

2. Create functions within the CFC


For those who are new to functions in CF.

You can write functions in CFML page:
<cffunction name="myFunction"  returntype="string" output="no">
    <!--- requires a string --->
    <cfargument name="myArgument" type="string" required="yes">
    <cfset myResult=myArgument>
    <!--- returns the same string --->
    <cfreturn myResult>
</cffunction>

Open in new window


And access them as below:
<cfoutput>
   #myFunction("Hi How are you")#
</cfoutput>

Open in new window


Now let's add this is a very simple function which accepts a string and returns the same string to our CFC.
<cfcomponent>
    <cffunction name="myFunction"  returntype="string" output="no">
          <!--- requires a string --->
          <cfargument name="myArgument" type="string" required="yes">
          <cfset myResult=myArgument>
          <!--- returns the same string --->
          <cfreturn myResult>
   </cffunction>
</cfcomponent>

Open in new window

3. Convert a function to web method to make the CFC a web service


This is a quite simple change, as all that is needed is to add access="remote" to our example function.
<!--- ... --->
   <cffunction name="myFunction" access="remote" returntype="string">
<!--- ... --->

Open in new window

4. Publish / test the web service


Now that we have a simple web service with a functioning web method, we need to place this component in an appropriate folder, map it in web server, and test.

To test, we access this file with your browser.  Since this is a component, it should be viewed with the CF component browser (see image 1.2) which will require you to log into CF administrator (see image 1.1) with your admin password.  You will see that MyFunction() is a method that takes a string argument and you can test from there.

5. Consume the web service


<cfinvoke
webservice="http://localhost/path/to/your.cfc?wsdl"
method="MyFunction"
returnvariable="myResults">
<cfinvokeargument name="myArgument" value="HI Srikanth"/>
</cfinvoke>
<cfoutput>
#myResults#
</cfoutput>


<cfscript>
myWebservice = CreateObject("webservice", "http://localhost/path/to/your.cfc?wsdl");
results = myWebservice.MyFunction(myArgument="HI Srikanth");
</cfscript>
<cfoutput>#results#</cfoutput>

Open in new window


Using Createobject  we  can consume our web service.  It takes two parameters: first, type which is webservice in our case; second, the URL to the Web Service Descriptor Language (WSDL) file which can be created by appending the wsdl query string to the CFC file name.
Please find more details about create object here http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt145.htm


If we registered the webservice in CF administrator then we can use that registered name here; however, we are going with full URL for simple test case.

We can also use cfinvoke to consume web service - Please find more details about cfinvoke here  http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt163.htm

With CFMX 7 when invoking the CFC as a web service be careful to name your file something different than the value of the displayname param.

If your file is ViewUsers.cfc do not set displayname="ViewUsers" otherwise you'll receive compile errors.  Also spaces do not make a difference so displayname="View Users" would also generate an error.


We are calling our function Myfunction and passing the parameter "HI Srikanth".
Which will result in printing "HI Srikanth" to the browser.

That's it!  We are done in creating and consuming an example web service.  Hopefully, you found this helpful.
img1.JPG
img2.JPG
2
Comment
0 Comments

Featured Post

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Join & Write a Comment

Check How effective MS Exchange Expert thinks Exchange Mailbox Recovery by SysTools IS. Visit the Official site to get detailed information:- https://www.systoolsgroup.com/exchange-recovery.html (https://www.systoolsgroup.com/exchange-recovery.h…
If you, like me, have a dislike for using Online Subscription anti-spam services, then this video series is for you. I have an inherent dislike of leaving decisions such as what is and what isn't spamming to other people or services for me and insis…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month