URLEncoded Arguments of a Remote Component Function
Monday, December 11th, 2006There are a number of ways to invoke functions of a ColdFusion component:
- <cfinvoke component="#myCfc#" method="helloworld">
- <cfargument name="input" value="Hello World!">
- </cfinvoke>
- <cfset myCfc.helloworld("Hello World!")>
- <a href="/my.cfc?method=helloworld&input=Hello%20World!">Invoke Component Function</a>
- <form action="/my.cfc" method="post">
- <input type="hidden" name="method" value="helloworld" />
- <input type="text" name="input" value="Hello World!" />
- <input type="submit" name="submit" value="Invoke Component Function" />
- </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.