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:
http://localhost:8500/rest/restapp/artService/getart/1
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:
http://localhost:8500/rest/restapp/artService/getart/
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])*.
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.
http://localhost:8500/rest/restapp/artService/getart/1
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:
http://localhost:8500/rest/restapp/artService/getart/
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.
Would you not just have two funtions,one with restpath="/getart"
ReplyDeleteAnd the other with restpath="/getart/ ...
@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.
ReplyDelete