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

File upload and Progress events with HTML5 XmlHttpRequest Level 2

The XmlHttpRequest Level 2 specification adds several enhancements to the XmlHttpRequest object. Last week I had blogged about cross-origin-requests and how it is different from Flash\Silverlight's approach .  With Level 2 specification one can upload the file to the server by passing the file object to the send method. In this post I'll try to explore uploading file using XmlHttpRequest 2 in conjunction with the progress events. I'll also provide a description on the new HTML5 tag -  progress which can be updated while the file is being uploaded to the server. And of course, some ColdFusion code that will show how the file is accepted and stored on the server directory.

Server sent events with HTML5 and ColdFusion

There are several ways to interact with the server apart from the traditional request\response and refresh all protocol. They are polling, long polling, Ajax and Websockets ( pusherapp ). Of all these Ajax and Websockets have been very popular. There is another way to interact with the server such that the server can send notifications to the client using Server Sent Events (SSE) . SSE is a part of HTML5 spec:  http://dev.w3.org/html5/eventsource/

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.