AsyncHTTP – Asynchronous HTTP Requests

I don't know how many times I've gone looking to do asynchronous processing in ColdFusion, and come up against the myriad of hacks, workarounds and that general feeling that I want to poke my eyes out with a rusty spoon.

Now that JavaLoader exists, integrating Java with ColdFusion becomes so ridiculously easy, it would have been relative heresy if I hadn't sat down for an afternoon and wrote a library for making asynchronous HTTP GET and POST requests.

To use the asyncHTTP library , you simply have to drop /asyncHTTP/ the folder in your web root, or create a mapping to it.

From here we create an instance of the AsyncHTTP.cfc:

asyncHTTP = createObject("component", "asyncHTTP.AsyncHTTP").init();

Now that we have an instance of the AsynHTTP CFC, to do an asynchronous GET request, we simply call:

asyncHTTP.get("http://www.site.com/mytemplate.cfm?var=value");

If we want to pass form data across, we can also do an asynchronous POST request, like so:

formData = StructNew();
formData.var = "value";
asyncHTTP.post("http://www.site.com/anothertemplate.cfm", formData);

That is it! It is as simple as that.

Please download the library, give is a spin, and any questions, feedback, bugs or general comments, please either reply to this blog post, or shoot me an email .

Leave a Comment

Comments

  • Jordan Clark | July 19, 2006

    Cool tool. In the past I’ve used CFHTTP but set the timeOut attribute to 1, so the request is fired off, and the response ignored (well after a 1 sec pause). Do you think making requests with your CFC would be faster? Also I’m curious why you created your own java object for the request instead of using the apache httpclient that comes packaged with coldfusion?

  • Mark | July 19, 2006

    Jordan,

    Good points all around –

    On the CFHTTP with the timeout, I often found this wasn’t that reliable (I may be wrong, but it just seemed that way), and I wanted a nice clean way to do this without having to basically hack your way through it.

    In regards to my Java Obect – I also assumed it was the apache httpclient with ColdFusion, but in fact, it’s not, it’s a different library found here:
    http://www.innovation.ch/java/HTTPClient/

    The AsyncHTTP lib does actually use this library, if you look at the Java source, you can see it being used throughout. It made making HTTP calls a breeze.

    Essentially AsyncHTTP is just a wrapper for this lib in a new java.lang.Thread, and a nice CFC wrapper over all that.

    Nothing fancy, but hopefully useful.

  • Kurt Wiersma | July 20, 2006

    The other day I was wondering if this same type of technique could be used with the Java CFC proxy to call CFCs asyncronizely on CF Professional.

  • Jordan Clark | July 20, 2006

    Hey I just had a great idea for your tool, what about adding the ability for the asych request to save the response to a file. I was just thinking about mining some amazon data, if I could save the REST xml response to a file I could fire off a bunch of downloading threads at once, without having to wait for each one to finish.

  • Mark | July 20, 2006

    Kurt:

    Unfortunatley, it can’t be done, as CFCProxy is limited to being used by Enterprise only, otherwise, you could do exactly that… however, let’s remember that CFC’s with remote methods can be called via URL… 😀

    Jordan:

    While an interesting idea, it would probably be better to fire off your async call to the page that does the mining.. which means you can do whatever you want in it. Doesn’t seem to be a need to build that sort of functionality you need in here.

  • Andy Allan | July 20, 2006

    Interestingly enough, you could use the CFCProxy with CF7.0 Standard. However, this was then restricted when 7.01 came out.

  • Rejith | October 31, 2017

    Hi,

    There is a problem with the links you have provided here, all are going to “http://www.compoundtheory.com/asynchttp-asynchronous-http-requests/”. I think the SEO url’s are messing with it. Is there anything you can do about that?