MitrahSoft is an Adobe offcial partner MitrahSoft is Lucee offcial partner & core contributing developer

REST API Implementation Using FW1 ColdFusion

REST (REpresentational State Transfer), is an architectural style for providing standards for communicate between various computer applications (Web, Mobile, IoT..etc). REST API is the future and it is de facto standard in modern SPA (single page applications) applications. here it explains the implementation of REST API using FW1 framework. You might consumed various famous REST APIs (google maps API, PayPal API etc.,) in our applications. But building a RESTful web service, like other programming skills is part art. In this blog post, we are going to learn, how to build a RESTful web service using FW/1 framework in Adobe ColdFusion or Lucee. FW1 is a ColdFusion lightweight MVC framework developed by Sean Corfield.

REST API implementation using FW1 ColdFusion

Process
  • At first, You have to create a simple FW1 ColdFusion Application (just a skeleton app for FW/1 framework, to create this just extends your Application.cfc to FW/1 core cfc).
  • Add RESTful routes to the routes settings in your FW/1 framework. Adding RESTful routes is nothing but, you are making your component as a REST resource. In REST Architecture everything is a resource. Each and every REST URL points to a particular resource and the HTTP method determines what to do with that resource (eg. GET to get data, POST to create a new object, PUT/PATH to update, DELETE to drop a resource).

Application.cfc

Here, 'mycomponent' is the controller name (cfc file name inside controllers folder) which you are going to use as an REST resource. Adding this simple setting means the following default settings.

Lets give a brief explaination for this. If your REST URL is pointing to mycomponent resource and the http method is GET then it will hit the default method in mycomponent.cfc. In the same way, If your REST URL is pointing to mycomponent resource and the http method is POST then it will hit the create method in mycomponent.cfc. From this, you can able to know clearly that your Resource (mycomponent.cfc) should contain the predefined methods (default, new, create, show, update, destroy). However, You can also able to overwrite these default routes & controller CFC method names.

myComponent.cfc

For these methods, passed 'id' value will be available in rc with the name relevant to their resource component(like mycomponent_id).

Here, renderData() is a FW1 framework helper method to bypass views and layouts completely and automatically return data rendered as JSON, XML, or plain text to your caller based on contentType mentioned in type chained method. Once you have called renderData(), you can do chain builder calls for the following methods to set their corresponding values.

  • data() to set the data payload to be rendered
  • type() to set the content type
  • header() to add an HTTP response header (this is an new feature in release 4.0)
  • statusCode() to set the HTTP status code
  • statusText() to set the HTTP status message (this is a new feature in release 4.0)
  • jsonpCallback() to set the JSONP callback
URL to access this endpoint
  • http://myapplication/index.cfm/mycomponent

Here, http://myapplication/index.cfm refers the path of your application and /mycomponent at the end refers the resource name (the name which we provided in { "$RESOURCES" = "mycomponent" }). As I mentioned earlier, based on the http method, it will call the corresponding method and render the data to the caller.

Get call sample

if run http://myapplication/index.cfm/mycomponent URL, it will be considered as get http method for getting collection of all resources and will access the corresponding default method to get the values.

Get call response
Get single value sample

if run http://myapplication/index.cfm/mycomponent/{:id} URL, it will be considered as get call for a single resource and will access the corresponding show method in your controller to get the detais about that particular resource details.

Get single value response
Other methods

Other methods like post, patch and delete can be called like below CFML code, to test it or you can use tools like postman too to test those.