RESTful PUT/DELETE with AS3 using X-HTTP-Method-Override

Posted on August 10, 2010

1


Flash/Flex doesn’t support PUT or DELETE requests, because the browsers don’t β€” due to limitations in the NPAPI. This is a source of frustration. Workarounds using sockets or prioxies are messy, don’t work with binary data or some other problem…

But fortunately, there seems to be a good (enough) solution!

Keenan, at Automata Studios, blogs about using an extra request header parameter (X-HTTP-Method-Override), a method that seems to be accepted by Google among others. Good news!

To submit a DELETE request from flash, all I have to do is setting the requestHeaders X-HTTP-Method-Override parameter with the value “DELETE”:

	var loader:URLLoader = new URLLoader();
	loader.addEventListener(Event.COMPLETE, onComplete);
	var request:URLRequest = new URLRequest("http://url/to/rest/server");
	request.method = URLRequestMethod.POST;
	request.data = "dummyData"; // Some data needed, otherwise sent as GET request!
	request.requestHeaders = [new URLRequestHeader("X-HTTP-Method-Override", "DELETE")];
	loader.load(request);

Using php, this can be handled on the server side like this:

$method = (isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'])) ? $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] : $_SERVER['REQUEST_METHOD'];
echo $method;

if the server parameter for HTTP_X_HTTP_METHOD_OVERRIDE is set, it picks the request method from that parameter value, otherwise from the normal REQUEST_METHOD value.

Not a truly genuine RESTful solution, but close enough! πŸ™‚ Thank you, Keenan!

Advertisements