Archive for December, 2006

URLEncoded Arguments of a Remote Component Function

Monday, December 11th, 2006

There are a number of ways to invoke functions of a ColdFusion component:

Code (coldfusion)
  1. <cfinvoke component="#myCfc#" method="helloworld">
  2.   <cfargument name="input" value="Hello World!">
  3. </cfinvoke>
  4. <cfset myCfc.helloworld("Hello World!")>
  5. <a href="/my.cfc?method=helloworld&input=Hello%20World!">Invoke Component Function</a>
  6. <form action="/my.cfc" method="post">
  7.   <input type="hidden" name="method" value="helloworld" />
  8.   <input type="text" name="input" value="Hello World!" />
  9.   <input type="submit" name="submit" value="Invoke Component Function" />
  10. </form>

In addition, you can invoke a function through Flash Remoting or as a Web Service.

I recently discovered that if you invoke a remote function through a URL or a form on a web page, the arguments are automatically URLDecoded for you before the function is invoked.

I had some input in a form that included a '+' character. The browser naturally encoded the '+' into '%2B' before posting to my component function, as it should. Then ColdFusion (behind the scenes) decoded the string back to '+' before calling my function.

The problem was that in my function, which I knew was being called from a form, I immediately called URLDecode() on my input, to convert any instances of '%xx' to the correct character. Well, a '+' represents a [space] in a URL-Encoded string, so the '+' that was in the original input ended up as a [space].

The moral of the story is to be aware that ColdFusion will automatically URLDecode arguments from the URL or form before invoking your remote component method being invoked directly.