Skip to main content

ColdFusion 10: Making REST Pathparams optional

I was working on creating a CRUD application with REST and wanted to retrieve records with a GET request. The GET request would contain the ARTID for which the record has to be retrieved in the URL itself. To do this I had added an argument to the method implementing the GET request with ‘restargsource’ attribute set to path. The URL would be of the form:


However, in another scenario I wanted to retrieve all the records from the ART table when there is no ARTID specified in the URI i.e. with the URL:


PathParams are always required and if they’re not specified then a ‘404 Not found’ error would be thrown. I thought of adding another method to handle this GET request with no PathParams. But, it would have made my code look clumsy and semantically not correct. However, I was able resolve this by providing a regular expression for the PathParam argument.

In REST, PathParams can have a regular expression assigned to them. For example, if a PathParam argument say 'name' would contain only alphabets then the regular expression [a-zA-Z] would match the restpath. Here the restpath attribute would be {name: [a-zA-Z]}. Similarly the regular expression for optional data would be ([0-9])*.

<cfcomponent restpath="/artService"> <cffunction name="getRequestHandler" access="remote" httpmethod="GET" returntype="query" produces="application/json" restpath="/getart/{artid: ([0-9])*}"> <cfargument name="artid" type="string" restargsource="path" required="false"/> <!--- execute when ARTID is specified in the path ---> <cfif len(arguments.artid) GT 0> <cfquery name="qdata"> select ARTID, ARTNAME, DESCRIPTION from ART where ARTID =<cfqueryparam value="#arguments.artid#"> </cfquery> <cfelse> <!--- execute when ARTID is not specified in the path ---> <cfquery name="qdata"> select ARTID, ARTNAME, DESCRIPTION from ART </cfquery> </cfif> <cfreturn qdata> </cffunction> </cfcomponent>

In the above code snippet the restpath is '/getart/{artid: ([0-9])*}'. If ARTID is not specified in the URL then all the records from the ART table would be fetched and returned to the client in application/json format. In case the ARTID is specified then only the records matching the specified ARTID would be returned to the client.


  1. Would you not just have two funtions,one with restpath="/getart"
    And the other with restpath="/getart/ ...

  2. @AJ Mercer , you're right that's another way of doing it. But what I wanted to demonstrate was to use regular expressions do define a pattern for the arguments. And of course not to repeat the code.

  3. Does it work in Railo??

  4. This is Adobe CF 10 specific only. I'm not sure whether Railo has the same implementation.

  5. This works with CF 2018, however this does not work with restplayground nor API Manager

    HTTP Status 400 – Bad Request
    Type Status Report

    Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

    Apache Tomcat/9.0.21


Post a Comment

Popular posts from this blog

A cheat sheet of keyboard shortcuts in ColdFusion Builder

In my last post I have explained about keyboard shortcuts in ColdFusion Builder 2.0. This blog post contains a list of all shortcut keys and I have listed these keyboard shortcuts based on the categories that it falls into. Well, this post was not planned since users can easily get to know the keyboard shortcuts by navigating to the preferences (ColdFusion -> Profiles -> Keys). However, I met Joshua at Scotch on the rocks in Edinburgh and he suggested that it would be nice to have the list of keyboard shortcuts handy. So this post is for those who would like to have the list with them and refer it whenever required.

Keyboard shortcuts for inserting text:

These are the keyboard shortcuts which are used to insert some text into the editor:

CommandKeyboard shortcuts on WindowsKeyboard shortcuts on MacInsert anchor tagCTRL + T, LCMD + T, LInsert bold tagCTRL + T, BCMD + T, BInsert br tagCTRL + T, RCMD + T, RInsert cfabortCTRL + T, ACMD + T, AInsert cfdumpCTRL + T, DCMD + T, DInsert cfs…

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…