title: How to POST custom object to web api using C#
body: <p>I have a web api developed and deployed. It works with unit test and using fiddler. However, if i create a separate project and try to post data to it, it sends back a 400 Bad Request error. I am looking for a way to post this custom object using a client being written in C#. Unlike WCF there is no matadata to create proxy of these objects from and then instantiate and pass it on to calling methods. How do you pass a custom object like Customer as a parameter to a method?</p>
  
  <p>URL would be <a href="http://host/directory/api/customer/AddNewCustomer" rel="nofollow">http://host/directory/api/customer/AddNewCustomer</a>
  and method definition is like </p>
  
  <pre><code>[HttpPost]
  public bool AddNewCustomer(Customer customerObj)
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Following is my client code</p>
  
  <pre><code>HttpClient client = new HttpClient();
  client.BaseAddress = new Uri("http://host/directory/");
  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  client.Timeout = TimeSpan.FromSeconds(30.0);
  
  // strong typed instance
  var values = new JObject();
  values.Add("FirstName", "John");
  values.Add("LastName", "Doe");
  
  HttpContent content = new StringContent(values.ToString(), Encoding.UTF8,"application/json");
  
  var response = client.PostAsJsonAsync("api/customer/AddNewCustomer", content).Result;
  </code></pre>