I am new to wcf restful service. I could not find the problem, which was why my wcf restful service gives a "bad request". I am using .NET 4.0.
My service:
[OperationContract(Name="Add")] [WebInvoke(UriTemplate = "test/", Method = "POST", ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json )] public int Add(Number n1) { res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2); return res; }
Data:
[Serializable] public class Number { public int Number1 { get; set; } public int Number2 { get; set; } }
When I call fiddler its return 'HTTP / 1.1 400 Bad Request
Title of my script title:
User-Agent: Fiddler Host: localhost:4217 Content-Type: application/json; charset=utf-8
And the request body:
{"Number1":"7","Number2":"7"}
And the response header:
HTTP/1.1 400 Bad Request Server: ASP.NET Development Server/10.0.0.0 Date: Sun, 14 Aug 2011 18:10:21 GMT X-AspNet-Version: 4.0.30319 Content-Length: 5450 Cache-Control: private Content-Type: text/html Connection: Close
But if I call this service using the C # client program, that's fine.
My client code:
uri = "http://localhost:4217/Service1.svc/"; Number obj = new Number() { Number1 = 7, Number2 = 7 }; using (HttpResponseMessage response = new HttpClient().Post(uri+"test/", HttpContentExtensions.CreateDataContract(obj))) { string res = response.Content.ReadAsString(); return res.ToString(); }
Please help me........
Thanks.
source share