Overview
ColdFusion comes bundled with added support for creating REST applications on websites via ColdFusion code. This is done by making ColdFusion components within your application REST-enabled. This article is designed to outline what is required to take advantage of the REST-enabled services available in ColdFusion.
Registering the REST Service
It is very simple to begin taking advantage of your own REST API in a ColdFusion web application. This can be done by utilizing the restInitApplication
method in your site’s Application.cfc
file. Below are tag and script-based examples of this method.
Tag-based:
<cffunction name="onApplicationStart">
<cfset restInitApplication("C:\home\website-name.com\api\","test-api")>
<cfset this.restsettings.usehost=true/>
<cfset this.restsettings.host="website-name.com"/>
</cffunction>
Script method:
restInitApplication(expandPath("/api/"), "test-api",{
skipCFCWithError : false,
useHost : true,
host : "website-name.com"
});
Once you’ve added this method to your Application.cfc you will then need to restart your application. This can be done by creating a CFM template that contains the following:
<cfset ApplicationStop() />
<cflocation url="index.cfm" addtoken="false" />
Note: The path to your REST-enabled application will need to contain rest-enabled components to call your API. Additionally, all API requests will require two service mapping references. The one you specify in your ‘serviceMapping’ attribute along with the /REST/ mapping. All requests to /REST/ are handled by the ColdFusion connector which is responsible for mapping your web request to the service mapping you’re specifying.
For example:
https://website-name.com/REST/test-api/[component name]/[function name]/
To take advantage of my rest-enabled CFC, I would have to include /REST/ and /test-api/ at the beginning of each request. From there you can reference your component path along with the function you want to invoke.
Congratulations! You have now created your first rest-enabled ColdFusion application and you can now begin taking advantage of your rest-enabled ColdFusion components. If you encounter any issues implementing a rest application on your website, then please feel free to contact our world class support team.