Does ColdFusion support REST API URIs with a dynamic token in the middle of the URI?

520 views Asked by At

I've been playing with ColdFusion 11's REST API support and am wondering if it's possible to have it support a URI with a dynamic token in the middle of the URI instead of only at the end. That is, it very easily supports URIs like:

/rest/users/12345

where the 12345 is dynamic (in this case, the user's userID). But I haven't been able to find a way (without a tremendous amount of URI hacking) to support URIs like:

/rest/users/12345/emailAddresses

So, is it possible to do this in ColdFusion (11 or 2016)? If not, is it supported in Taffy (I didn't see where it is but I could be wrong)?

TIA

1

There are 1 answers

0
queevert On

It's been a while and I wanted to provide the answer in case anyone else has this same question...

ColdFusion, when defining a CFC for a REST endpoint, allows you to specify wildcards/variable names in the restpath attribute to both the <cfcomponent> and <cffunction> tags. You would then define <cfargument> tags for each one of these variables so that you can access them within your function. For example:

<cfcomponent rest="true" restpath="/users/{userId}/pets" ... >
    <cffunction name="getPets" access="remote" httpMethod="GET">
        <cfargument name="userId" type="numeric" required="true" restargsource="Path" />

        <!--- Called with a path like /users/123/pets/ --->
        <!--- do stuff using the arguments.userId (123) variables --->
    </cffunction>

    <cffunction name="getPet" access="remote" httpMethod="GET" restpath="{petId}">
        <cfargument name="userId" type="numeric" required="true" restargsource="Path" />
        <cfargument name="petId" type="numeric" required="true" restargsource="Path" />

        <!--- Called with a path like /users/123/pets/456/ --->
        <!--- do stuff using the arguments.userId (123) and/or arguments.petId (456) variables --->
    </cffunction>
</cfcomponent>

The keys here are using the restpath attribute with the variable defined as a variable name in curly braces and then defining those variables as arguments to the function with a restargsource attribute set to "Path".

I hope this helps.