1

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?

URL would be http://host/directory/api/customer/AddNewCustomer and method definition is like

[HttpPost]
public bool AddNewCustomer(Customer customerObj)

EDIT

Following is my client 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;
Salman
  • 3,137
  • 4
  • 23
  • 31
  • Are you sure you are sending a post request and not some other kind? – KnightFox Jul 11 '15 at 19:15
  • Have you tried this? http://stackoverflow.com/questions/4088625/net-simplest-way-to-send-post-with-data-and-read-response – Rosdi Kasim Jul 11 '15 at 23:17
  • Please check with fiddler that you are actually sending your JSON with `Content-Type: application/json` in the header along with a `Content-Length: {lengthOfYourContent}`. They are required for the WebApi to handle them successfully – Martin Jul 13 '15 at 08:35
  • Also you should be able to use Newtonsoft `JsonConvert.SerializeObject(yourObject)` it will give you a json string value of your object, which can be used in your `StringContent` – Martin Jul 13 '15 at 08:45

2 Answers2

3

After trying many solutions, usign fiddler i came to know that content object was not set. This is strange but that was the case. So i tried passing JObject directly wihout casting it to HttpContent and it worked

// strong typed instance
var values = new JObject();
values.Add("FirstName", "John");
values.Add("LastName", "Doe");

// this is not set!!!
HttpContent content = new StringContent(values.ToString(), Encoding.UTF8,"application/json");

var response = client.PostAsJsonAsync("api/customer/AddNewCustomer", values).Result;
Salman
  • 3,137
  • 4
  • 23
  • 31
1

Ideally Customer would be in a separate class library of models and you could share that dll between projects. But any object with the same property names will work because web API will serialized them to Json or xml and make the mappings automatically.

Crowcoder
  • 11,250
  • 3
  • 36
  • 45
  • Ok, thats exactly what i am thinking. I have tried using JObject with similar property names abd still getting same response. Can you please share any code snippet if possible? – Salman Jul 11 '15 at 17:07
  • Post your client code where it creates the object and makes the request. – Crowcoder Jul 11 '15 at 17:18
  • Does the result of values.ToString() match what you were putting into Fiddler? – Crowcoder Jul 12 '15 at 00:35