Friday, December 31, 2010

HTTP PUT & DELETE from an Actionscript REST client

REST based APIs use the HTTP verbs - POST, GET, PUT and DELETE to accomplish Create, Read, Update, and Delete operations on data respectively. Flash player for various reasons only supports GET and POST. This makes writing REST apps a little harder (an additional line on the client side, and support for an extra request header on the server end) than it should be.


Thankfully, most popular web frameworks provide some support to accomplish this. Ruby on Rails supports overriding the http method using the X-HTTP-Method-Override request header.

The below code tells the RoR server app to consider the request as a PUT operation:

var request:URLRequest = new URLRequest(resource);
request.data = dataToUpdateOnResource;
request.method = URLRequestMethod.POST;
request.requestHeaders.push(new URLRequestHeader("X-HTTP-Method-Override", "PUT" URLRequestMethod.PUT));
var loader:URLLoader = new URLLoader();
loader.load(request);

HTTP DELETE can be achieved using the same.

No comments: