Invoking the DELETE verb with the Mootools Request.JSON class

Recently, I was trying to use the DELETE verb on a URL (using the MooTools Request.JSON class), and found (using Firebug) that the even though I was invoking the request correctly, the actual request going out was a POST. The code looks something like this.

var req = new Request.JSON(
{
    method: “DELETE”,
    url: url,
}); 

req.addEvent("failure", failurefn);
req.addEvent("success", successfn);
req.send(args);
On debugging I found that the following piece of code in the MooTools library’s Request class (which is the base class for Request.JSON)
if (this.options.emulation && !['get', 'post'].contains(method)){
    var _method = '_method=' + method;
    data = (data) ? _method + '&' + data : _method;
    method = 'post';
}

was converting the DELETE request into a POST request, and adding the actual method (DELETE in this case as part of the POST data. It appears that if you want to send any other verb besides a GET or POST as the actual HTTP verb using the Request class, you must set the “emulation” option to false. This is documented in the MooTools documentation for the Request class.

image

The value of the emulation option defaults to true in the class. So my code now looks something like this (notice the emulation: false):

var req = new Request.JSON(
{
    method: method,
    url: url,
    emulation: false
}); 

req.addEvent("failure", failurefn);
req.addEvent("success", successfn);
req.send(args);

And everything works as expected.

Advertisements

About floatingfrisbee

A programmer/blogger from New York City
This entry was posted in Mootools, Uncategorized, web services and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s