FormUrlEncodedContent with support for array values

Posted by & filed under Dev', Tips.

As convenient as System.Web.Http is for .NET developers, while working on my current project, I discovered that it did not support array values out of the box.
When you want to post something in the ‘x-www-form-urlencoded’ form, and using the FormUrlEncodedContent class, you are limited to string keys and values as FormUrlEncodedContent is build out of an IEnumerable<KeyValuePair<string, string>> (or more commonly a Dictionary<string, string>) object.

What if I want to send an array like :
POST http://dev.someserver.fr/0.2/theaters/set HTTP/1.1
Accept: */*
Content-Length: 40
Accept-Encoding: identity
Connection: Keep-Alive
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
User-Agent: NativeHost
Host: dev.cinetime.fr
Pragma: no-cache
theaters=["LI38249","LI20271","LI20306"]

Well you can’t. Or you could rewrite and extend FormUrlEncodedContent.
Or you could just reuse the following gist, because that’s precisely what I’ve done :