Railo ReST API - Doing somthing with the Resource

 

For a given URL, multiple actions can be performed. The action is specified by the METHOD of the request. Web developers will be familiar with GET and POST. The HTTP spec defines many more such as PUT, DELETE and OPTIONS.

So for /rest/api/person/123 the GET, PUT and DELETE methods can be used. For /res/api/person the GET and POST methods can be used. OPTIONS can, and should be, used on any URL. Anyone dealing with CORS will need to understand OPTIONS. 

The CRUD would look something like the

    remoteany function getPerson
        (
            required string id restargsource="Path"
        ) 
        restpath="" 
        httpmethod="GET"
        {}
    remote any function postPerson
        (
        ) 
        restpath="" 
        httpmethod="POST"
        {//new}        
    remote any function getPersonByID
        (
            required string id restargsource="Path"
        ) 
        restpath="/{id}" 
        httpmethod="GET"
        {}
    remote any function putPersonByID
        (
            required string id restargsource="Path"
        ) 
        restpath="/{id}" 
        httpmethod="PUT"
        {//update}        
    remote any function deletePerson
        (
            required string id restargsource="Path"
        ) 
        restpath="/{id}" 
        httpmethod="DELETE"
        {}    

PUT can be used to update  resource, but if ID is provided, will create the resource with that URL.

PATCH or MERGE can be used to just to update fields passed in rather than the whole record like a PUT would.

When naming the functions I put use the method as a part of the name ( I also include mime-type as well). The name of the function is not important to the ReST request, but like all functions, they must be unique within the CFC. To date, Railo does not through an error if the functions have the same name, but will use that last one - this can be a trap for cut-n-paste developers :-S

I cover the OPTIONS method in its own tutorial - coming soon...