Overriding returnFormat at runtime

This post is more than 2 years old.

Ok, so this falls in the "Not so sure this is a good idea" department. Stefan Vesterlund posted a comment on my last blog entry concerning returnformat. He asked if it was possible to change the returnFormat at runtime. I said that I didn't think it was possible, but that you could simply use returnFormat="plain" and return JSON or WDDX manually. He played around with it and discovered you could override the default behavior. Consider the following code sample.

<cffunction name="testMethod" access="remote"> <cfset var a = [1,2,4,9]> <cfreturn a> </cffunction>

Since I've specified no returnFormat in the cffunction tag, if I hit this via the browser and don't override returnFormat in the query string, the result will be a WDDX string. Now consider this version:

<cffunction name="testMethod" access="remote"> <cfset var a = [1,2,4,9]> <cfif a[1] is 1> <cfset url.returnFormat="json"> </cfif> <cfreturn a> </cffunction>

The code now checks the first entry in the array, and if the value is 1, it sets url.returnFormat. Surprisingly this works. I guess ColdFusion doesn't bother worrying about the returnFormat settings until the end of the method, which kinda makes sense.

I still feel weird about this one, but thought I'd pass it along.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com