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;
4
  • Are you sure you are sending a post request and not some other kind? Commented Jul 11, 2015 at 19:15
  • Have you tried this? stackoverflow.com/questions/4088625/… Commented Jul 11, 2015 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 Commented Jul 13, 2015 at 8: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 Commented Jul 13, 2015 at 8:45

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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?
Post your client code where it creates the object and makes the request.
Does the result of values.ToString() match what you were putting into Fiddler?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.