Yui Compressor issues with reserved words in JavaScript

For the life of me, I could not figure out why I was getting an error while running the Yui compressor on a set of my JavaScript files.

The error was very generic and not helpful

error : Failed to read in the data for the path/file [..\<path>\<filename>]. The most common cause for this is because the path is incorrect or the file name is incorrect … so please check your path and file names. Until you fix this up, I can’t continue … sowwy.

The file was present and seemed to be syntactically correct and all.

Finally, thanks to a little squiggly placed by Resharper (which is awesome!!), I found the issue. One of the js functions was called “goto” and that, apparently is reserved by JavaScript for “future use”, and Yui bombs on it.

Renaming it fixed the issue.


To be clear, the YUI compressor is behaving as it was designed to. Reserved words should not be used in JavaScript, as in other languages. However, I will say that the error dumped out when the compressor runs into a reserved word could be better.

Yui Compressor issues with reserved words in JavaScript

Crazy Idea of the Day – Product Placement in Google Streeview

I was looking up the street view for the Met, and this is what the view from Columbus Avenue looks like. Fresh Direct guys are every where in NYC but not so sure this was unintentional! That’s some prime product placement for a great NYC Startup.

Just FYI, this is on 02/20/2012.

Crazy Idea of the Day – Product Placement in Google Streeview

SQL Server 2008 – Creating a database from a .bak file

I’m not a database expert!

Every so often I run into something that seems so simple that should just work but doesn’t. Searching for solution is a tedious chore of sifting through jargon and unclear explanations, somehow more so in database land than with other technologies… what is it with these “database types”?

Anyways, creating a database from a .bak file was one of those things. In SSMS, it should really be as simple as

1. Choose the .bak file
2. Give a name to the destination database
3. Press Go

But it’s not. Yes, there are probably good reason for it… actually no, all those good reasons are just travesty!

By default, you can only restore databases from .bak files that were created from them. If you try to do something else, you will get the following error.

Similarly if you try to restore using T-SQL

FROM DISK = 'c:\DatabaseBackups\dev2012021601.bak'

You will see the following error if the database “newdatabase” already exists

Msg 3154, Level 16, State 4, Line 1
The backup set holds a backup of a database other than the existing 'jb' database.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

or something like this one if it doesn’t already exist

Msg 1834, Level 16, State 1, Line 1
The file 'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\devdb.mdf' cannot be overwritten. It is being used by database 'devdb'.
Msg 3156, Level 16, State 4, Line 1
File 'dbname' cannot be restored to 'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\devdb.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 1
The file 'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\devdb_1' cannot be overwritten. It is being used by database 'devdb'.
Msg 3156, Level 16, State 4, Line 1
File 'devdb_log' cannot be restored to 'c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\devdb_1.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

If that’s where you are stuck, the following will help you.

1. In SSMS, open  a query window in the master database of the database server. That’s where you will run the following queries.

2. See what the “LogicalName” of the database that has been backed up in the .bak file is 

FROM DISK = 'c:\DatabaseBackups\dev2012021601.bak'

This will give you the logical name of the database and its associated log file. Lets assume that the names are “dbname” and “dbname_log”

3. Now run the following restore command. Make sure that a database with the name you are trying to create doesn’t already exist (in the code sample below, dbForSocialMigration doesn’t exist).

RESTORE DATABASE dbForSocialMigration 
FROM DISK = 'c:\DatabaseBackups\dev20120307.bak' 
MOVE 'dbname' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\dbForSocialMigration.mdf', 
MOVE 'dbname_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\dbForSocialMigration_log.mdf'

 C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA is the directory where SQL Express usually keeps its data files. You can find the directory your database server is using by selecting a database from it, right clicking and opening the properties dialog and selecting the “Files” option from the left.

That should work, and at that point you should be able to access the database “dbForSocialMigration” populated with all the tables and data from the .bak file.

Good Luck!

SQL Server 2008 – Creating a database from a .bak file

Uploading a file to Amazon S3 using an ASP.NET MVC application directly from the user’s browser

Associated Code

The code associated with this post can be found at the link below (you can also download a .zip file of the code from this link).


15th February 2013 Update

There is a limitation in the current implementation in that it does not allow for the FileId to be set to the actual name of the file, and instead uses a pre-generated GUID as the FileId. This was pointed out to me by “DE” in the comments below.

The reason is that the signature is generated before the view is rendered and a file is selected. If the FileId is updated later the upload will fail because the signature will not match the signature that Amazon will compute. This can be remedied (I think) by capturing the onchange event on the file input in the view, and make an ajax call to generate the signature at that time instead of pre-computing it. I’ll work on it if I find time, but anyone is free to fork the Git repo and make that fix!

18th January 2013 Update

A lot of people have asked me if I have the associated code, and I finally was able to find some time to put it up on Git. Here is the link


Remember to open web.config and put in your AWS key, secret and bucket id. Besides that it should just run as long as you can run an ASP.NET MVC 4 application.

Original Post

Amazon S3 is a service where you can store large (or small) files. It is organized in terms of “buckets” so before you can store any thing under your account, you have to create a bucket first (or have access to an existing one). Then you can specify the bucket you want to upload the file into, along with other required pieces of information and even your own metadata.

That is a great solution for a lot of web applications, specially those that are already utilizing Amazon’s web services. And being able to upload directly to Amazon S3 from the user’s browser is important because it saves bandwidth and CPU cycles for you as the application owner and time for everyone involved.

Obviously there are a couple of issues here!

1. How do you get a user’s browser to bypass your web application, and upload a file directly to Amazon S3’s storage.

2. How do you ensure that the user’s browser communicates only with Amazon S3 and not an impersonator.

3. How do you ensure that none of the parameters of the upload; things like bucket name, the file’s ACL on S3, and other metadata you might specify, are changed by the user or any intermediary during this process (since the upload is not going though your application).

Fortunately there are solutions for each of the above and really at the end of the day it boils down to the same ol’ trick that most cryptography relies on: symmetric and asymmetric keys. I’ll present the code I used to implement this using an ASP.NET MVC application using C# right after I explain the solution in the far more complicated language of English.


1. This one is simple. An HTML form that has a “method” of “post” and an “action” that points to the correct Amazon S3 URI for your bucket will do the trick. Off course the form also needs to have an “input” of type “file” and an “input” of type “submit”. Besides this all the other parameters of the upload; both those that are required by Amazon S3 and others that you might want to store for your purposes, are added as hidden “input” fields to the form.

2. This one is simple too. Just ensure that the form’s action is specified with “https” as the protocol.

Something like this

https://<your bucket name>.s3.amazonaws.com

This will cause the browser to use SSL to communicate with Amazon and the usual handshake, certificate lookup and encryption that goes along with it will ensue.

3. The solution for this is more complicated and judging from the buzz on the web is a source of frustration for many a web developers, and more so for ASP.NET developers because there are not many good code examples available online. The solution boils down this; create a string that has all the parameters of the upload, and then hash that string using your Amazon S3 private key and the HMAC SHA1 algorithm. Upload both that string with the upload parameters and the hash generated using the private key with the HTML form created in step 1, as hidden “input” fields. When Amazon S3 receives the form, it will de-hash the hash (since they have access to your private key), and compare it to the upload parameter string, and to the actual upload parameters. If they match up, they know that the upload parameters were not tampered with, otherwise, they will reject the upload. No one else can generate the correct hash because no one else has your Amazon S3 private key (well, at least that you know about!).

There you have it. Now…

Lets move on to the ASP.NET MVC  implementation

I was using MVC3 and Razor as my view engine. If you’re new to ASP.NET MVC, you probably want to spend a little time understanding the basics.

First the ViewModel

The members of my ViewModel class are basically the things that the view needs to create the form with it hidden and visible fields, and the form action, method and encapsulation type.

public class FileUploadViewModel 
    public string FormAction { get; set; } 
    public string FormMethod { get; set; } 
    public string FormEnclosureType { get; set; } 
    public string Bucket { get; set; } 
    public string FileId { get; set; } 
    public string AWSAccessKey { get; set; } 
    public string RedirectUrl { get; set; } 
    public string Acl { get; set; } 
    public string Base64Policy { get; set; } 
    public string Signature { get; set; } 

Next the code to generate an instance of the ViewModel

You could package this code as a part of another library or as a part of the helpers in your MVC solution but for this example, I’m not going to get into that.

The key part here is off course the generation of the policy string and then generating the signature, which involves taking the hash of the policy string using your Amazon S3 private key and the HMAC SHA1 hashing algorithm. Having a foundational understanding of encodings and string formats is helpful as always. As Joel and many others have said, you should familiarize yourself with base64 and also what a strings and “encoding” mean, specially in .NET.

First lets look at the policy string. The policy string (also called the policy document or the “Access Control” document) must adhere to the rules specified in Amazon S3’s documentation. You should familiarize yourself with the syntax of the policy string described in it. In the end for my case it ended up looking like like this.

    "expiration": "2011-04-20T11:54:21.032Z", 
    "conditions": [ ["eq", "acl", "private"], ["eq", "bucket": "myas3bucket"], ["eq", "$key", "myfilename.jpg"], ["content-length-range", 0, 20971520], ["eq", "$redirect", "myredirecturl"]] 

I used a class marked with the[DataContract] attribute and used the DataContractJsonSerializer to serialize it into the right format. Beats constructing the string in code.

Now lets look at how you would generate the signature. You will probably store the private key in web.config like this

    <add key="AWSAccessKey" value="MyAmazonS3AccessKey"/>
    <add key="AWSSecretKey" value="MyAmazonS3SecretKey"/>
    <add key="AWSBucket" value="MyAS3Bucket"/>

and retrieve it like this before passing it to the method to create the signature.

string publicKey = ConfigurationManager.AppSettings["AWSAccessKey"]; 
string secretKey = ConfigurationManager.AppSettings["AWSSecretKey"]; 
string bucketName = ConfigurationManager.AppSettings["AWSBucket"];

Now let’s look at the “Signature”. In Amazon S3’s documentation it is defined as

Signature is the HMAC of the Base64 encoding of the policy

The function below creates the “Signature”. The parameters are “normal” C# strings. Meaning no additional encoding information is being implied. I have made the function more verbose than normal to clarify the steps involved.

private string CreateSignature(string secretKey, string policy) 
    var encoding = new ASCIIEncoding(); 
    var policyBytes = encoding.GetBytes(policy); 
    var base64Policy = Convert.ToBase64String(policyBytes); 
    var secretKeyBytes = encoding.GetBytes(secretKey); 
    var hmacsha1 = new HMACSHA1(secretKeyBytes); 
    var base64PolicyBytes = encoding.GetBytes(base64Policy); 
    var signatureBytes = hmacsha1.ComputeHash(base64PolicyBytes); 
    return Convert.ToBase64String(signatureBytes); 

Let’s walk through the function. First I am converting the policy string into a series of bytes. To do that you must choose an encoding (because different encodings lead to a different series of bytes for the same string). I choose ASCII because that’s what I found was being used in some of the PHP and Ruby samples I came across. Apparently that is correct because this works.

Once you have the series of bytes for the policy string, you need to convert it into a base64 string, which really means reinterpreting the series of bytes in 6 bit chunks to ensure each chunk stays within a certain range. Anyways, .NET provides the handy Convert.ToBase64String function to deal with that.

Once you have the base64 encoded policy string, you need to hash it. To do that you create an instance of the .NET HMAC SHA1 hasher, supplying the bytes of the secret key as a parameter to its constructor. Again we get the bytes of the private key using the ASCII encoder.

Now we use that hasher to hash the policy bytes. The result of that is the set of bytes of the signature which can then be converted into the base64 encoded string and returned.

Base64 encoding has to be used to transmit both the policy string and the generated hash because base64 is a way to guarantee that any kind of characters will be first converted into the ASCII range. That, as I have read, is a fairly common practice.

So in the end the code to generate the view model ends up looking like this

public FileUploadViewModel GenerateViewModel(string publicKey, string secretKey, string bucketName, string fileName, string redirectUrl) 
     var fileUploadVM = new FileUploadViewMode(); 
     fileUploadVM.FormAction = string.Format(“https://{0}.s3.amazonaws.com/”, bucketName);
     fileUploadVM.FormMethod = “post”; 
     fileUploadVM.FormEnclosureType = “multipart/form-data”; 
     fileUploadVM.Bucket = bucketName; 
     fileUploadVM.FileId = fileName; 
     fileUploadVM.AWSAccessKey = publicKey; 
     fileUploadVM.RedirectUrl = redirectUrl; // one of private, public-read, public-read-write, or authenticated-read 
     fileUploadVM.Acl = “private”; // Do what you have to to create the policy string here 
     var policy = CreatePolicy(); 
     ASCIIEncoding encoding = new ASCIIEncoding(); 
     fileUploadVM.Base64Policy = Convert.ToBase64String(encoding.GetBytes(policy)); 
     fileUploadVM.Signature = CreateSignature(secretKey, policy); 


Finally the View

The view, like I said is using the Razor engine, and simply lays out a “form” that has all the relevant fields Amazon S3 requires and optionally others you care about.

@model TheApp.ViewModels.FileUploadViewModel
    ViewBag.Title = “Upload File”; 
    Layout = “~/Views/Shared/_Layout.cshtml”; 

<div id=”fileuploaddiv” class=”fileuploaddivclass”> 
    <form action=”@Model.FormAction” method=”@Model.FormMethod” enctype=”@Model.FormEnclosureType”> 
         <input type=”hidden” name=”key” value=”@Model.FileId” /> 
         <input type=”hidden” name=”AWSAccessKeyId” value=”@Model.AWSAccessKey” /> 
         <input type=”hidden” name=”acl” value=”@Model.Acl” /> 
         <input type=”hidden” name=”policy” value=”@Model.Base64EncodedPolicy” /> 
         <input type=”hidden” name=”signature” value=”@Model.Signature” /> 
         <input type=”hidden” name=”redirect” value=”@Model.RedirectUrl” /> 
         <div> Please specify a file for upload: <input type=”file” name=”file” size=”100″ /> </div> 
         <input type=”submit” value=”Upload” /> 

Hopefully this helps. Feel free to leave feedback and questions.

Uploading a file to Amazon S3 using an ASP.NET MVC application directly from the user’s browser

So how many ways can you…

I get obsessive about math puzzles. Not crazy ones that will take some sort of Aryabhata to figure out but little interesting ones that require intuition and a little obsession to crack. Probability and its puzzles fall right in this zone and most often, books and courses will teach people the formulae but not really instill an understanding of why things work the way they do. So… I decided to put some information down in a post. It is mostly in the form of definitions of basic concepts and clarifications of what those basics really mean.

So here goes…


This is the set of things you have available to you, to choose from. So if you have a bag with 2 red balls and 3 green balls, then those 5 balls are your universe. If you have 10 kinds of candy bars from which you can choose, say 4, that set of 10 candy bars is your universe.

Events and Outcomes

Events are something you are trying to achieve at a higher level, and outcomes are the different ways in which you can achieve that higher level objective or event.

For example if a problem asks you to choose 3 vegetables out of a bag of 10 vegetables, the event is the picking of any 3 vegetables. The outcomes that go to make that event are however many more,

10C3 = 10!/(3! * 7!) = 120

to be precise.

Similarly if you are trying to pick out 2 Aces from a pack of 52 cards, your event is exactly that, picking out of any 2 Aces, where as the outcomes that enable that event are 6 in number,

4C2 = 4!/(2! * 2!)  = 6

“With Replacement” Vs. “Without Replacement”

If you have a universe of things to choose from (letters, numbers, balls, whatever…), and after you choose one of the things in the universe, you do not put it back into that universe, it’s called “Without Replacement”. If you do put it back, it’s called “With Replacement”. Another way to look at it is that you modify the universe every time you pick one thing in the “Without Replacement” case, and do reset the universe to what it was originally in the “With Replacement” case.

“Order Matters” or “Order Does not Matter”

In some problems, the order in which you extract things out of your universe is significant (think about a lottery number, the order of numbers picked is important). In some other cases, the order in which you extract things may not be important (think about the case where you need to pick 3 toys from a basket of toys – usually what order those toys will be picked out is not important).

Whether order is important or not is critical in determining how many ways (or more formally outcomes), there are to satisfy a certain event. To clarify this, lets say I have to figure out how I can extract the letters ‘A’ and ‘D’ from a universe, (‘A’, ‘B’, ‘C’, ‘D’). In other words, my event is the case where ‘A’ and ‘D’ have been extracted out from the universe. If order is important, the number of outcomes is 2 – (‘A’, ‘D’) and (‘D’, ‘A’). If order is not important, the number of outcomes is one, because we are not differentiating between the case where ‘A’ was picked first and ‘D’ last from the case where ‘D’ was picked first and ‘A’ last.

As a rule of intuition, when order is not important you have much fewer outcomes to satisfy the same event.

The Math

There really is not much math involved in this kind of problems; it’s really about understanding the question, and then imagining  and categorizing the solution. Once you have done that, to save time you can just use one of the existing formulae to give you an answer and save some time.


n! = n * (n-1) * (n-2) * … 3 * 2 * 1


If there is no replacement, and you care about the order in which you extract items out of your universe, then the number of ways, or outcomes, you have to extract `r` items from a universe containing `n` items is represented by the expression


and can be computed as

nPr = n!/(n-r)!


If there is no replacement, and you do not care about the order in which you extract items out of your universe, then the number of ways, or outcomes, you have to extract `r` items from a universe containing `n` items is represented by the expression


and can be computed as

nCr = n!/(r! * (n-r)!)

As mentioned above, intuitively it should be clear that if you don’t care about the order of extraction, you will have much fewer outcomes. And this formula confirms that intuition.

Bringing it Together

Ok, with those basics under your belt, lets try an example that brings together many of these concepts.


If a menu has 10 different dishes available, and any customer is allowed to choose 4 of them, how many combinations of dishes must the chef be prepared to make, in each of the following scenarios.

  1. If the customer can choose a dish only once, and the order they pick the dishes in is relevant (meaning “Wonton soup, Lomein, Pot  Sticklers, and General Tso’s Chicken” and “Lomein, Pot Sticklers, Genral Tso’s Chicken and Wonton Soup” are to be regarded as 2 separate combinations).
  2. If the customer can choose a dish only once, and the order they pick the dishes in is irrelevant (meaning “Wonton soup, Lomein, Pot  Sticklers, and General Tso’s Chicken” is the same as “Lomein, Pot Sticklers, Genral Tso’s Chicken and Wonton Soup”; the order is irrelevant).
  3. If the customer can choose a dish multiple times, and the order they pick the dishes in is relevant (meaning “Wonton soup, Lomein, Pot  Sticklers, and General Tso’s Chicken” and “Lomein, Pot Sticklers, Genral Tso’s Chicken and Wonton Soup” are to be regarded as 2 separate combinations).
  4. If the customer can choose a dish multiple times, and the order they pick the dishes in is irrelevant (meaning “Wonton soup, Lomein, Pot  Sticklers, and General Tso’s Chicken” is the same as “Lomein, Pot Sticklers, Genral Tso’s Chicken and Wonton Soup”; the order is irrelevant).


Before getting into each case, lets look at the question as a whole. The following is a break up of what’s supplied as a part of the question:

Universe: The set of 10 dishes

Event: The picking of 4 dishes

When the customer can pick one dish only once, it’s really a way to say there is no replacement. When the customer can pick the same dish multiple times, its a way of saying that there is replacement.

Ok, now lets look at each case

    1. This is simply the number of permutations in which 4 things can be picked from 10.
10P4 = 10!/(10-4)! = 5040
    1. This is simply the number of combinations in which 4 things can be picked from 10.
10C4 = 10!/(4! * (10-4)!) = 210
    1. Since there is replacement here, or in other words the customer can pick the same dish one, two, three or four times, we have many more ways in which 4 dishes can be picked. Also order is relevant, meaning every different combination is valid. So the total number of ways that 4 dishes can be picked out of 10 is
10 * 10 * 10 * 10 = 10,000

Basically at every point you have the option of picking any of the 10 dishes and you have 4 of these opportunities.

    1. This is the most interesting of the 4 scenarios. Like in (c) you have 10 dishes to choose from all 4 times. However order is not relevant and hence those outcomes where the same things were picked have to be eliminated. Lets start with the answer we have in (c), 10,000. If from those you delete all outcomes that included a dish only once treating order as relevant (a) and then add back the outcomes that included a dish only once treating order as irrelevant (b) you get the following:
10,000 – 10P4 + 10C4
=> 10,000 – 10!/6! + 10!/(4! * 6!)
=> 5170

However, in that total you still have the outcomes are the same (though their order of dishes is different) that included 1 repeat, 2 repeats and 3 repeats, and they need to be removed from that total. If you figure that one out, post the solution in a comment!

Here’s a bonus question, and something I will probably ask you if I ever interview you for a software engineering position. (Don’t worry, i’ll be able to figure out whether you knew it before hand 🙂 )

A spider eats at most 3 flies a day, and stops trying to catch more flies once he has eaten 3 for the day. Until he fills his quota, he has a 50% chance of catching any fly that pass by his web. What are a fly’s chances of survival, given that five 5 have passed by the spider’s web today?

So how many ways can you…

Netflix should tie up with Movie Theaters

I love watching movies, and in New York City, where I live, the cost of watching one movie in a theater is around $13-$15. For two people, that’s almost $30, and if you have a family of four, that’s pretty darn pricey.

No wonder lesser and lesser people are “going to the movies”. Well, there are other reasons too I guess. Home theater systems for one, but I don’t know if their penetration has reached the point that they are causing the masses to stay home. Shitty movies might be another, but shittyness doesn’t have that great a track record of keeping the masses away from anything. I mean we have this and this! Maybe piracy? Now that’s a whole new conversation, but at the end of the day I do believe; provide something compelling and people will pay (a little).

I was in a bus passing by the United Artists Theater on 1st Ave & 85th St, and its currently playing Black Swan, and thought hmm, it would be good to get off the bus and go watch it until my impulse to not pay $30 to some production house kicked in. You can argue if that is justified, but afford it or not, it doesn’t pass my cost-benefit analysis-o-meter!

So here’s my idea; I googled it a bit and doesn’t seem like its been talked about publicly. Netflix, feel free to steal and implement it. Okay, maybe not really. If it works, I would like 50% of the profits. Just putting it out there in the universe, you never know what sticks!

I currently pay somewhere around $10 to get one DVD at home and unlimited streaming of the movies available for instant watching. Pretty good deal, as my cost-benefit analysis-o-meter tells me. Charge me $20, if you can also give me the ability to go to a theater and pay a small amount, say $5 and see a movie.

There are currently around 20 million Netflix subscribers in the US and Canada, and here is how much the top grossing movies of 2010 made. Table copied from here.

Rank Movie Title (click to view) Studio Total Gross /Theaters Opening / Theaters Open Close
1 Toy Story 3 BV $415,004,880 4,028 $110,307,189 4,028 6/18 12/2
2 Alice in Wonderland (2010) BV $334,191,110 3,739 $116,101,023 3,728 3/5 7/8
3 Iron Man 2 Par. $312,433,331 4,390 $128,122,480 4,380 5/7 8/19
4 The Twilight Saga: Eclipse Sum. $300,531,751 4,468 $64,832,191 4,468 6/30 10/21
5 Harry Potter and the Deathly Hallows Part 1 WB $292,890,256 4,125 $125,017,372 4,125 11/19

So i’m not really a finance guy but maybe there is a way to make the numbers work here. Infact, there will probably be a lot of interest in such an offer, even at a higher price point, say $30. Besides it will create economic activity that could be tied back to Netflix, and the movie business in the long run.

I don’t care about the shitty butter covered corn and sugar they serve at the theater, and yea, noisy idiots do bother me, but i’ll figure that one out. However, I do love to watch movies on the big screen, and will jump at this offer. Hopefully many others will too.

Finally I would like to say that these opinions are solely mine and I do own a minuscule amount of stock in Netflix, so if their share price was to increase because of this post, maybe I will go and watch a couple more movies at the local theater.

Netflix should tie up with Movie Theaters

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);
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.


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);

And everything works as expected.

Invoking the DELETE verb with the Mootools Request.JSON class