Sean put up his wishlist for CF recently, and one of the interesting things he was wishing for was the capability to call CF from the command line.
The thing is, you can already do this, and fairly easily. Let’s start by getting the right tool for the job - go pick up cURL, a tool for invoking URLs from the command line. Do note that if you choose to download the HTTPS-enabled version of cURL, you will need to also get the libeay.dll library for SSL support from OpenSSL.
That’s really all you need. You can invoke cURL like so: curl (CFM template URL). The output of the CFM is printed to the stdout, which means it will show up in your DOS window/UNIX shell/whatever. Easy, huh?
To invoke a CFC, you’ll have to use the CFC HTTP invocation syntax, which is simple enough:
http://(hostname):(port)/(context, if JEE deployment)/(path to CFC)?method=(function to invoke)&(argument list)
Responses from CFC functions are returned in WDDX format, unless the function returns XML, in which case the XML is returned as-is. Oh, and do make sure you tag your function with access=remote so that CF will allow remote calls to it.
For example, to invoke a function called echo, which takes an arguments input1 and input2, on a CFC my.application.callme, on a standalone CF deployment, you could do this:
http://myhost/my/application/callme.cfc?method=echo&input1=yodel&input2=hey
For those who think all this is just too hard (yeah, right!), here’s a simple shell script that will do the job of invoking a CFC - edit it to change your serverroot, then invoke it at will, passing in the CFC, function and argument list (in the form arg1=val1&arg2=val2).
serverroot="http://localhost:8500/" callto=`echo $1 | sed -e 's/\\./\\//g'` cffunction=$2 args=$3 url=$serverroot$callto".cfc?method="$cffunction"&"$args curl $url
For example, if your save your script as callCF.sh:
callCF.sh my.application.callme echo input1=yodel&input2=hey
Apologies to DOS users - the scripting that I have done, I’ve done on UNIX systems. Even when I’m forced to use Windows on the desktop, I always have Cygwin installed so I have all those great UNIX command line tools handy.
Now I hear you thinking - do we really have to have a server running to make all these invocations? Couldn’t we have a nice lightweight runtime that can be invoked on the fly? In theory, yes, of course we could. In practice, absolutely not - CFMX was built on the assumption that it would run within a JEE application server. Ripping out, and rebuilding, all the plumbing would be a prohibitively expensive task.
Well, there’s at least one wish fulfilled that we can all talk about - if only the others were as easy! I cannot, alas, yet tell you how many more of Sean’s, and others’, wishes will be granted for Scorpio… But rest assured, we’re paying attention. And some of those wishes, well, they’ve already been granted.
Update: Dopefly opines, as Sean does, that a gateway would provide for more secure invocation. I don’t entirely agree with them - this really comes down to where you choose to put your security, in a gateway, or in a CFC. It could easily be embedded in a CFC by checking that the CGI.SERVER_NAME is localhost, or by ensuring that the IP address of the caller, CGI.REMOTE_ADDR, is in a whitelist of IP addresses allowed to invoke the CFC.

Build It Smarter | 31-May-07 at 8:19 pm | Permalink
Calling CF from a command line?…
While looking over Ashwin Matthew’s ColdFusion blog posts, I caught this gem on calling CF from a command line interface (CLI). I have always wanted to use this exact feature but always presumed that it wasn’t possible. But now that……
Jeremy Rottman | 01-Jun-07 at 7:36 pm | Permalink
Very cool topic, I have been asked this very thing quite a few times.
One thing to point out. Depending on your version of sed and your distro you may need to alter your sed command some.
For example, on centos 5.* running sed 4.1.5 you have you escape any special chars with \.
So your callto var would look something like this.
callto=`echo $1 | sed -e ’s/\./\//g’`
Ashwin | 02-Jun-07 at 4:36 am | Permalink
Thanks, Jeremy. I think the escaping with \ is a generic requirement - for some reason my blog editor stripped them out, and I didn’t notice. Fixed now.
Jeremy Rottman | 05-Jun-07 at 5:29 pm | Permalink
Actually you can escape with any char you want to. It just has to be the first char trailing s.
However, with certain versions of sed on certain distro’s it truly only works with \
IE
callto=`echo $1 | sed `s*\.*/*g`
Ashwin | 06-Jun-07 at 4:05 am | Permalink
Thanks again, Jeremy - I didn’t know that the escape character could be specified to sed.
Javier Julio | 18-Jan-08 at 9:02 pm | Permalink
@Ashwin
I’ve set up a sample CFC to test out this feature and it works great. I’ve noticed that if an Application.cfm or Application.cfc (with onRequestStart) exists those files/functions are called. I was wondering then if onMissingMethod would fire if I did not type in a message in the query string?
Ashwin | 18-Jan-08 at 11:26 pm | Permalink
Javier - if I remember right, onMissingMethod will work fine, just so long as you provide a method name to invoke via the ‘method’ attribute on the URL; a plain URL without a method attribute may fail. Try it and do let me know how it works out. Sorry I can’t provide a definitive answer - it’s been over 6 months since I last looked at the CF source code!
Javier Julio | 13-Feb-08 at 5:27 pm | Permalink
Unfortunately, it does not work. Sorry I have replied so late. I thought I had made my comment elsewhere. I meant to say “method” in the previous comment by the way instead of “message”.
I’ve set the onMissingMethod function to both remote and public but I’ll still get a CF error saying my method was not found.
Is it possible that this could be added in via an updater or maybe the next release of CF? It’s a great addition to be able to call CFC methods directly via the URL. I love this CURL example since everyone here at my job uses it to interact with the services we build.
Ahamad | 25-Aug-08 at 10:44 am | Permalink
Hi,
I have posted a simple example on onMissingmethod here.
http://cf-examples.net/index.cfm/2008/8/25/onMissingMethod-Example