Skip to main content

ColdFusion 10: Using HTTP Content Negotiation to invoke a REST service

The HTTP protocol provides a Content negotiation mechanism using which different formats of the document can be served using the same URI. For example, a JavaScript application can request the content in JSON format and an external system say a Java client can request for the same content in XML format. Here the clients need to specify the content format in the Accept attribute of the HTTP request. The REST service can specify the format in which the data will be returned to the client in ‘produces’ attribute.

In ColdFusion, if you want to retrieve content in either JSON or XML format then the same can appended to the URI i.e. the URI will be of the form:

http://localhost:8500/rest/contentNeg/customerService/1.json

or

http://localhost:8500/rest/contentNeg/customerService/1.xml

Observe that the content-type is specified at the end of the URI (json\xml). ColdFusion would parse the URI and will invoke the corresponding REST service. The below code lists the two services that produce the data in JSON and XML format:
<cfcomponent rest="true" restpath="/customerService"> <cffunction name="pathHandlerJSON" access="remote" returntype="query" httpmethod="GET" produces="application/json" restpath="{customerID}"> <cfargument name="customerID" required="true" type="string" restargsource="path"/> <cfset resultQuery = retrieveCustomerRecord(arguments.customerID)> <cfreturn resultQuery> </cffunction> <cffunction name="pathHandlerXML" access="remote" returntype="query" httpmethod="GET" produces="application/xml" restpath="{customerID}"> <cfargument name="customerID" required="true" type="string" restargsource="path"/> <cfset resultQuery = retrieveCustomerRecord(arguments.customerID)> <cfreturn resultQuery> </cffunction> <cffunction name="retrieveCustomerRecord" returntype="query"> <cfargument name="customerID" type="numeric" required="true"/> <cfset myQuery = queryNew("id,name", "Integer,varchar", [[1, "Sagar"], [2, "Ganatra"]])> <cfquery dbtype="query" name="resultQuery"> select * from myQuery where id = #arguments.customerID# </cfquery> <cfreturn resultQuery> </cffunction> </cfcomponent>
In the above functions pathHandlerJSON and pathHandlerXML, a query is returned in either JSON or XML format. If you observe the only difference between the two functions is that the 'produces' attribute is different. Complex data types in ColdFusion are converted to either JSON or XML format and sent to the client.

As mentioned earlier the client can specify the desired Content-Type in the Accept header of the HTTP request. For example, a ColdFusion client can send a CFHTTP request with the Accept header specified in the CFHTTPPARAM tag:

<cfhttp url="http://localhost:8500/rest/contentNeg/customerService/1" method="get" result="resultJSON"> <cfhttpparam type="header" name="Accept" value="application/json"> </cfhttp> <cfdump var="#deserializeJSON(resultJSON.filecontent)#">
When making request through cfhttp, the expected content-type can be anything (text/html, text/plain etc,.) and can be specified in the cfhttpparam tag. Only JSON and XML can be specified in the URI. The HTTP result is de-serialized to obtain the below output:
REST_Response_JSON
Similarly other complex data types in ColdFusion can be serialized to either JSON or XML format. I’ll explain this in coming posts.

Comments

  1. Oh, these codes seem to be so much complicated... I wonder if I will have to manage all them manually or is there an easier way to get it all arranged?

     how to edit a picture

    ReplyDelete
  2. alonso , which codes you're referring to? You can specify the content-type in the URL or in the ACCEPT header.

    ReplyDelete

Post a Comment

Popular posts from this blog

De-obfuscating javascript code in Chrome Developer Tools

I had blogged about JavaScript debugging with Chrome Developer Tools  some time back, wherein I have explained how these developer tools can help in debugging javascript code. Today Google Chrome 12 was released and my Chrome browser was updated to this version. As with every release, there have been some improvements made on performance, usability etc,. One feature that stood out for me is the ability to De-obfuscate the javascript code. What is Minification? Minification is the process of removing unnecessary characters such as white spaces, comments, new lines from the source code. These otherwise would be added to make the code more readable. Minifying the source code helps in reducing the file size and thereby reducing the time taken to download the file. This is the reason why most of the popular javascript libraries such as jQuery are minified. A minified jQuery file is of 31 KB in size where as an uncompressed one is about 229 KB. Unfortunately, debugging minified javascript f

Adding beforeRender and afterRender functions to a Backbone View

I was working on a Backbone application that updated the DOM when a response was received from the server. In a Backbone View, the initialize method would perform some operations and then call the render method to update the view. This worked fine, however there was scenario where in I wanted to perform some tasks before and after rendering the view. This can be considered as firing an event before and after the function had completed its execution. I found a very simple way to do this with Underscore's wrap method.

On GraphQL and building an application using React Apollo

When I visualize building an application, I would think of using React and Redux on the front-end which talks to a set of RESTful services built with Node and Hapi (or Express). However, over a period of time, I've realized that this approach does not scale well when you add new features to the front-end. For example, consider a page that displays user information along with courses that a user has enrolled in. At a later point, you decide to add a section that displays popular book titles that one can view and purchase. If every entity is considered as a microservice then to get data from three different microservices would require three http  requests to be sent by the front-end app. The performance of the app would degrade with the increase in the number of http requests. I read about GraphQL and knew that it is an ideal way of building an app and I need not look forward to anything else. The GraphQL layer can be viewed as a facade which sits on top of your RESTful services o