Our favorite number is 1 (or 0)

Flash and REST


07.09.10 Posted in Blog by Keenan

When it comes to consuming web services, especially in Flash, most developers don’t think about using any other HTTP methods besides GET and POST.  In fact, I’m guilty of assuming that GET and POST are always the right way to go, and that other HTTP methods are just fluff for programmers with way too much time on their hands.  This narrow view of consuming data over HTTP even extends to the platforms we develop against, as evident by the fact that as of Flash Player 10.1, there is still no native support for proper communication with true RESTful web services (though all plugins suffer from this).

Now, you might be thinking, “I’ve never had a problem calling methods on a RESTful API in Flash. I just use URLLoader and URLRequest and it works just fine.”  If I had a dollar for every time I wrote code that worked but turned out to be wrong, I’d be writing this from my villa in Aruba.  My understanding of REST principles, like that of many other developers, was incomplete, and led to the misguided belief that I can consume all web services over HTTP the same way.  We know about GET and POST, but what about PUT and DELETE, core methods in a RESTful API.

Here are some good links that explain RESTful web services and the difference between POST and PUT as HTTP methods.

The problem in Flash is that the URLRequest class only supports HTTP GET and POST, and there is no way to use PUT or DELETE.  In fact, trying to do so results in a run-time error.  Steve Webster, developer for Yahoo, said it best in his 2008 blog post, “This is due to a limitation in the NPAPI pseudo-standard used by non-IE browsers that only exposes the ability to make GET and POST requests. Unless NPAPI is updated to allow other HTTP methods, and browser vendors and plugin authors update their implementations to match that new API, we need to find other ways of interacting with RESTful web services.”

In order to get around this problem (it’s not just a Flash problem), a lot of RESTful services out there, including Google APIs, are implementing an extra HTTP Request header, “X-HTTP-Method-Override”.  The value of this request header overrides the actual HTTP method used such that you can simply POST and state in the override header that you wish to use the PUT or DELETE method.  For example:

var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://yoursite.com/api/user/20/");
request.method = URLRequestMethod.POST;
request.requestHeaders = [new URLRequestHeader("X-HTTP-Method-Override", "DELETE")];
 
loader.load(request);

The block of code calls the User service’s DELETE method to remove the user with ID 20.  Using the X-HTTP-Method-Override request header, you can perform PUT, DELETE, and other HTTP methods (like HEAD, OPTION, etc.), despite the fact that Flash does not support them.



2 Responses to “Flash and REST”

  1. Useful info for REST noobs like myself, thanks. Looks like a typo on code line 4, since requestHeaders is an Array.

  2. Branden says:

    Good catch Robert – I fixed it and will be teasing Keenan appropriately on Monday!

Leave a Reply