title: C# application/x-www-form-urlencoded OAuth and HTTP REST requests using HttpClient
body: <p>So I'm trying to put together some very simple and elegant code samples to help people use my API.  The latest language I'm tackling is C#.</p>
  
  <p>I think the IETF OAuth2.0 standard I read implies the HTTP request Content-Type must be "application/x-www-form-urlencoded".  The Django API server I have, currently seems to only support this Content-Type (for the OAuth resource).  The other languages POST content this way by default!</p>
  
  <p>After extensive research and several experiments I am wondering if I have missed something fundamental.  Surely there would be a helpful library OR technique to create the ...urlencoded string OR at least someone else must have run into this???</p>
  
  <p>I will outline some of the best solution I have so far bellow, but it just seems wrong.</p>
  
  <p>Also from a bunch of internet browsing I figured I would use the HttpClient library.  I like the fact that it uses the async model, which perhaps will be more useful for any developers using WPF or XAML or Windows 8 apps.  It also works well for Consoles and Forms.</p>
  
  <p>I use the Newtonsoft Json.Net library for serialization.  First of all I create a POCO of the authorization strings.  Then I serialize it to JSON, then to key/value pairs, then iterate through the key/value pairs catenating with the required '=' and '&amp;' chars, then UTF-8, then escape the spaces etc.</p>
  
  <pre><code>//Setup HTTP request
  HttpClient httpRequest = new HttpClient();
  httpRequest.DefaultRequestHeaders.Add("Accept", "application/json");
  string urlBase = "https://__secret__/api/v1/";
  HttpResponseMessage msg = new HttpResponseMessage();
  
  //POST to oauth to get token (must be sent as "application/x-www-form-urlencoded")
  OAuthConfig oAuthCredentials = new OAuthConfig { client_id = client_id, client_secret = secret, username = "__secret__", password = "__secret__", grant_type = "__secret__" };
  string jsonString = JsonConvert.SerializeObject(oAuthCredentials);  //convert to JSON
  Dictionary&lt;string, string&gt; values = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, string&gt;&gt;(jsonString);  //convert to key/value pairs
  string urlEncodedData = ConvertToFormUrlEncodedFormat(values);
  HttpContent payload = new StringContent(urlEncodedData, Encoding.UTF8, "application/x-www-form-urlencoded");
  msg = httpRequest.PostAsync(urlBase + "oauth/access_token/", payload).Result;
  string responseBodyAsText = msg.Content.ReadAsStringAsync().Result;
  Console.WriteLine(responseBodyAsText);
  </code></pre>
  
  <p>Other options I could think of were...</p>
  
  <p>Reflection, however digging into reflection in a code sample seems a bit manic.</p>
  
  <p>It turns out that other than the OAuth setup, the rest of the API supports JSON type POSTs.  So I guess I could write a function to walk through the oAuthConfig model and catenate a string, since that POCO model is unlikely to change and is the only model which requires urlEncoding that I can anticipate.  That would save us from the slightly confusing use of the Dictionary.  The use of the dictionary and then iterating is more generic, however perhaps a bit OTT.</p>
  
  <p>I figure most of the time people will be using JSON, so showing how that might happen is useful.</p>
  
  <p>In general it seems quite hard to serialize a POCO model to a string and/or to a urlEncoded string.</p>
  
  <p>Questions</p>
  
  <ul>
  <li>does OAuth mandate urlencoded, could you convert server-side?</li>
  <li>is HttpClient the best choice, does async matter?</li>
  <li>is there a better simpler way to serialize a POCO to ...form-urlencoded?</li>
  </ul>
  
  <p>Thanks for any useful comments you may have.</p>