title: Calling MVC4 WebAPI methods from C# Metro UI Client using PostAsync, HttpClient & Json
body: <p>I've created a method using the new WebAPI features in MVC4 and have it running on Azure. The method requires that you post a simple LoginModel object that consists of a Username and Password property. Yes, I plan on securing this further once I get past this speed bump :-) The method then responds with an object in Json format:</p>
  
  <p><img src="https://i.stack.imgur.com/tpq75.jpg" alt="enter image description here"></p>
  
  <p>I can successfully call this method using Fiddler, provided that I include "Content-Type: application/json" in the Request Headers. It returns with 200 and I can go into the Fiddler Inspector and view the Json response object just fine:</p>
  
  <p><img src="https://i.stack.imgur.com/PBWJl.jpg" alt="enter image description here"></p>
  
  <p>I am however having problems calling this same method from a MetroUI app in Windows8 using C#/XAML. I began playing around with the HttpClient and the new Async concepts in C# and no matter how I formatted my Post calls (even when explicitly calling out that I want the Content-Type to be "application/json") Fiddler returns with a 500 error and states that the attempt was using Content-Type:"text/html". I beleive this to be the root of the problem:</p>
  
  <p><img src="https://i.stack.imgur.com/VnKDk.jpg" alt="enter image description here"></p>
  
  <p>I have tried everything conceivable in order to post to this method and get back the Json object, here is my most recent attempt:</p>
  
  <pre><code>HttpClient client = new HttpClient();
          client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
          HttpContent content = new StringContent(@"{ ""Username"": """ + Username.Text + @", ""Password"": """ + Password.Text + @"""}");
  
          client.PostAsync("http://myapi.com/authentication", content).ContinueWith(result =&gt;
          {
              var response = result.Result;
  
              response.EnsureSuccessStatusCode();
          });
  </code></pre>
  
  <p>This results in a 500 error with Content-Type set to "text/html"</p>
  
  <p>Here was another attempt which also failed:</p>
  
  <pre><code>HttpClient httpClient = new HttpClient();
  HttpResponseMessage response = await httpClient.PostAsync("http://myapi.com/authentication", new StringContent(@"{ ""Username"": """ + Username.Text + @", ""Password"": """ + Password.Text + @"""}", Encoding.UTF8, "application/json"));
  string statusCode = response.StatusCode.ToString();
  </code></pre>
  
  <p>Can anyone point me in the right direction?</p>
  
  <p>Just tried the following code thanks to the advice of Nemesv:</p>
  
  <pre><code>HttpClient httpClient = new HttpClient();
          HttpContent content = new StringContent(@"{ ""Username"": """ + Username.Text + @", ""Password"": """ + Password.Text + @"""}");
          content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
  
  
          HttpResponseMessage response = await httpClient.PostAsync("http://webapi.com/authentication", content);
  
          string statusCode = response.StatusCode.ToString();
          response.EnsureSuccessStatusCode();
  </code></pre>
  
  <p>It now shows "application/json" in my request header, but still shows "text/html" in the Web Session:</p>
  
  <p><img src="https://i.stack.imgur.com/EqtK3.jpg" alt="enter image description here"></p>