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
  3. I appreciate your efforts. This is really great work. Thank you for sharing these points. Keep up the good work you are doing here. Likewise check here: Free Horoscope

    ReplyDelete

Post a Comment

Popular posts from this blog

Custom validation messages for HTML5 Input elements using the constraint validation API

HTML5 has introduced several input types such as EMAIL, URL, RANGE, SEARCH, DATE, TIME, etc,. Most of the modern browsers have implemented them and are ready to be used in a HTML document. Another exciting feature introduced in HTML5 is the form validation. Instead of writing JavaScript to validate users input, browsers can now validate it and show an appropriate message if the validation fails. The validation message is shown in line with the field for which the validation has failed. The default error message is shown when the validation fails. In this post I'll explain how these error messages can be changed.

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 file…

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.