0

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

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
queevert
  • 33
  • 6
  • What happens when you call `/rest/users/12345/emailAddresses`? – Miguel-F Apr 21 '16 at 20:15
  • It seems to be supported. See the section 'Specifying subresources' here - https://helpx.adobe.com/coldfusion/developing-applications/changes-in-coldfusion/restful-web-services-in-coldfusion.html – Miguel-F Apr 21 '16 at 20:34
  • @Miguel-F Depends on the code. Using the built-in ColdFusion REST support, you build CFCs for each endpoint in your API. To "register" a specific CFC as an endpoint, you would use something like: `` This CFC would be called via `/rest/users`. In the CFC, you configure functions to accept URL params, as in: ` ` This would allow you to call `/rest/users/12345`. I can't see how to have anything after the "12345". – queevert Apr 21 '16 at 20:36
  • @Miguel-F Sorry if I'm being thick here but I don't see anything in that section that would allow me to support dynamic tokens in the middle of the URI. Can you elaborate on that a bit? Thanks. – queevert Apr 21 '16 at 20:45
  • I'm a bit unfamiliar with CF's built in but I don't see a way to do it as simple as Taffy. If I'm reading the docs right though if you use the Subresource locator you can technically do what's in the first example in the subresource section but it seems like it would be more cumbersome. Again, I'm less familiar with it, but it seems possible however I think you'd have to have some messy code. I know it's not your preferred, but if you have some standard you could use URL Rewriting. Very little work as long as you have some common paths. – Leeish Apr 21 '16 at 22:28
  • I misunderstood your question, sorry for the confusion. Perhaps you should look at using query string parameters instead. Something like this - http://stackoverflow.com/a/16134855/1636917 – Miguel-F Apr 22 '16 at 13:50
  • @Leeish Thanks for the comment. I was kind of hoping that I wouldn't need to use URL Rewriting because CF would have it built-in. But it looks like that's not the case and that I may need to go with URL Rewriting. – queevert Apr 22 '16 at 16:15
  • @Miguel-F Thanks for the link. My hope is to use query string parameters to restrict the scope/order/etc. of what is returned but use the path parameters to identify the resource. The big problem with doing all of this by query string is that it would necessitate the code on the other end of the URI to be able to handle all of the different "resources" that are defined on the query string. In other words, 1 big CFC to handle everything instead of several smaller CFCs each with a specific purpose and/or for a specific resource. It's looking more and more like I'll have to go with URL Rewriting. – queevert Apr 22 '16 at 16:21

1 Answers1

1

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.

queevert
  • 33
  • 6