title: Generic POST request using Microsoft.HttpClient and HttpContentExtensions
body: <p>I am using the extremely awesome HttpClient provided in the WCF REST Starter Kit. I have the following method that is working against the HelloTxt API:</p>
  
  <pre><code>public UserValidateResponse Validate()
  {
      HttpClient client = new HttpClient(baseUrl);
  
      HttpMultipartMimeForm form = new HttpMultipartMimeForm();
      form.Add("app_key", this.AppKey);
      form.Add("user_key", this.UserKey);
      HttpResponseMessage response = client.Post("user.validate", form.CreateHttpContent());
  
      return response.Content.ReadAsXmlSerializable&lt;UserValidateResponse&gt;();
  }
  </code></pre>
  
  <p>I have a nice generic GetRequest method that looks like this:</p>
  
  <pre><code>public T GetRequest&lt;T&gt;(string query)
  {
      HttpClient client = new HttpClient(baseUrl);
      client.DefaultHeaders.UserAgent.AddString(@"http://www.simply-watches.co.uk/");
  
      HttpResponseMessage response = client.Get(query);
      response.EnsureStatusIsSuccessful();
  
      T data = default(T);
      try
      {
          data = response.Content.ReadAsXmlSerializable&lt;T&gt;();
          return data;
      }
      catch (Exception ex)
      {
          Console.Write(String.Format("{0}: {1}", ex.Message, ex.InnerException.Message));
      }
  
      return data;
  }
  </code></pre>
  
  <p>The benefit of which is that you can pass it T as the response type as per this random example:</p>
  
  <pre><code>public List&lt;User&gt; GetUsers(int deptid)
  {
      string query = String.Format("department.getUsers?api_key={0}&amp;dept_id={1}", this.APIKey, deptId);
  
      return GetRequest&lt;List&lt;User&gt;&gt;(query);
  }
  </code></pre>
  
  <p>I now want to the same generic style POST method, rather than GET and I'm sure I can use the HttpContentExtensions, but I can't figure out how to transform the request into a HttpMultipartMimeForm. this is what I have so far:</p>
  
  <pre><code>public T PostRequest&lt;K, T&gt;(string query, K request)
  {
      HttpClient client = new HttpClient(baseUrl);
      // the following line doesn't work! Any suggestions?
      HttpContent content = HttpContentExtensions.CreateDataContract&lt;K&gt;(request, Encoding.UTF8, "application/x-www-form-urlencoded", typeof(HttpMultipartMimeForm));
  
      HttpResponseMessage response = client.Post(query, content);
      response.EnsureStatusIsSuccessful();
  
      T data = default(T);
      try
      {
          data = response.Content.ReadAsXmlSerializable&lt;T&gt;();
          return data;
      }
      catch (Exception ex)
      {
          Console.Write(String.Format("{0}: {1}", ex.Message, ex.InnerException.Message));
      }
  
      return data;
  }
  </code></pre>
  
  <p>It would be called like this:</p>
  
  <pre><code>UserValidateResponse response = PostRequest&lt;UserValidateRequest, UserValidateResponse&gt;("user.validate", new UserValidateRequest(this.AppKey, this.UserKey));
  </code></pre>
  
  <p>It is to work against this API: <a href="http://hellotxt.com/developers/documentation" rel="noreferrer">http://hellotxt.com/developers/documentation</a>. Any suggestions are extremely welcome! I could define a different form for each POST, but it would be nice to do this generically.</p>