Skip to main content

ColdFusion 10: Understanding REST parameters

As mentioned in my earlier post there are various ways in which data can passed to a REST service. The cfargument tag has a new attribute restargsource, it can take one of these values – path, query, matrix, form, cookie and header.

Path Param:
Here you can pass arguments to a REST service by specifying the same in the URL path. The URL may look like:

http://localhost:8500/rest/restapp/customerService/CF/Zeus

Here two values ‘CF’ and ‘Zeus’ are passed as an argument to the REST service. The function defined as a REST service in CFC will have two arguments and the functions restpath would have two parameters:
<cffunction name="pathHandler" access="remote" returntype="string" httpmethod="GET" produces="text/plain" restpath="{productName}/{productCodeName}"> <cfargument name="productName" required="true" type="string" restargsource="path"/> <cfargument name="productCodeName" required="true" type="string" restargsource="path"/> <cfreturn productName & " " & productCodeName> </cffunction>
Observe that the restpath attribute in the above function has two values separated by a slash ‘/’  i.e. ‘{productName}/{productCodeName}’. The values ‘CF’ and ‘Zeus’ are copied to these parameters. Another variant would be to pass a hyphen(-) in the restpath in which case the URL would end with CF-Zeus and the function has to updated with its restpath attribute set to {productName}-{productCodeName}.

Query Param:

Query params are the most commonly used way to pass data to a REST service by specifying the parameters in URL after question mark(?). The URL would look like this:

http://localhost:8500/rest/restapp/productService?productName=ColdFusion&productCodeName=Zeus

Unlike Path parameters, the name of the argument is specified in the URL itself and the arguments are separated with an ampersand(&). The function to handle this service need not have the restpath attribute defined since the argument is passed as a query parameter in the URL. The other change would be to change the restargsource value to ‘query’.

Matrix Param:

Similar to Query param, in matrix param the arguments are passed to a REST service by specifying same in the URL where the arguments are separated by semicolon(;). Therefore the URL would look something like this:

http://localhost:8500/rest/restapp/productService;productName=ColdFusion;productCodeName=Zeus

Observe that there is no ? symbol used in the URL. Instead a semicolon is used. Here the function providing REST service must have its arguments' restargsource set to ‘matrix’.

Cookie and Header Param:

In case of Cookie and Header param the data is sent in the HTTP request. While sending a REST service request, same can be included by specifying it in the chttpparam tag.
In some cases where the variable name may contain a hyphen(-). For example, the request header may contain variable fname-lname. Now on the server side the argument name cannot be fname-lname because it’s not a valid identifier. To handle this there is an attribute 'restargname'. Here, you can specify the argument name as anything say ‘fname_lname’ and restargname as ‘fname-lname’. The value passed to fname-lname will be mapped to fname_lname.

Form Param:

In many scenarios, data entered in the form has to be processed by a REST service. This data can be used to make a new entry (POST) or update an existing record (PUT) in the database. If form fields are used then the restargsource should be set ‘form’. This will extract the data present in request and will be available for further processing. Also, the content-type header has to be set to ‘application/x-www-form-urlencoded’ when sending data as form fields.

Comments

  1. Can you comment on why someone would use "matrix" style? It seems like just an alternate version of regular URL query params.

    ReplyDelete
  2. Raymond Camden I don't see any difference other than better readability.

    ReplyDelete
  3. There is difference between matrix and URL params.

    In query params, the params always comes at the end after the "?". 

    http://localhost:8500/rest/restapp/productService?productName=ColdFusion

    But in a URI, matrix params can come in between also. For eg:-

    http://localhost:8500/rest/restapp/productService;productName=ColdFusion/feature;name=ORM

    ReplyDelete
  4. Yes, as mentioned in the post matrix params use semicolon(;) as a separator and query params use ? and &. 

    And you are also right with specifying matrix params anywhere in the URL, but this is not supported in ColdFusion.

    ReplyDelete

Post a Comment

Popular posts from this blog

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.

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

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