Implementing ETag with Railo ReST Web Services

May 30 2013

Implementing ETag with Railo ReST Web Services

Posted by AJ Mercer at 1:07 PM CFML | Railo

This is a quick How-to for implamenting Entity Tag ( HTTP ETag) with Railo ReST web services; partially for Furture Self, but may help others too ;-)

remote any function getSomeResource
  (
    required string ResourceID restargsource="Path"
             string IfNoneMatch restArgName="If-None-Match" restargsource="header"
  ) 
  httpmethod="GET" 
  restpath="/something/{ResourceID}" 
  produces="application/json" 
  output=false
{
  ...
  var someData = GetData();
  var ETag = Hash(serialize(someData));
  header name="ETag" value="#ETag#"

  if (ARGUMENTS.IfNoneMatch != '' AND ARGUMENTS.IfNoneMatch == ETag) {
    status = 304; // Not Modified
    results['results'] = [];
  }
    else {
      status = 200; // OK
      results['results'] = someData;        
  }
  ...
  RestSetResponse({"status":"#status#"});
  return results;
}

The ETag is passed by the client in Header 'If-None-Match'. Notice the use of restArgName in arguments - this enables you to map to a CFML variable.

I have set the ETag header using Railo's ability to simple drop the CF and angle brackets off a tag. You can also use the RestSetResponse function. It appears that the web browser will set If-None-Match if the previous responce has the ETag header.