How to call wcf restful service from fiddler on JSON request?

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.

+6
source share
2 answers

As you learn about a problem with the code, enable tracing on the server, and there will be an exception explaining the problem. I wrote a simple test with your code and had a similar error (400), and the traces had the following error:

 The data contract type 'WcfForums.StackOverflow_7058942+Number' cannot be deserialized because the required data members '<Number1>k__BackingField, <Number2>k__BackingField' were not found. 

The data types marked [Serializable] serialize all the fields in the object, not the properties . Commenting on this attribute, the code really works fine (the type then falls into the POCO (Plain Old Clr Object) rule, which serializes all public fields and properties.

 public class StackOverflow_7058942 { //[Serializable] public class Number { public int Number1 { get; set; } public int Number2 { get; set; } } [ServiceContract] public class Service { [OperationContract(Name = "Add")] [WebInvoke(UriTemplate = "test/", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] public int Add(Number n1) { int res = Convert.ToInt32(n1.Number1) + Convert.ToInt32(n1.Number2); return res; } } public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior()); host.Open(); Console.WriteLine("Host opened"); WebClient c = new WebClient(); c.Headers[HttpRequestHeader.ContentType] = "application/json; charset=utf-8"; c.Encoding = Encoding.UTF8; Console.WriteLine(c.UploadString(baseAddress + "/test/", "{\"Number1\":\"7\",\"Number2\":\"7\"}")); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); } } 
+8
source

One thing you do wrong is the format of the request object. Chang is:

 { n1: { "Number1":"7","Number2":"7"} } 

Parameter names are property names of the json object root passed to the wcf json endpoint.

I have a sample wcf service on git. Among other things, it has a json endpoint, and in Default.asmx I use jquery to call. I would suggest you create a similar web page (host it on the same website as the WCF service) that calls your json service through jquery and tests this service while the script is running to get a sample request. This will be simpler and easier to error than creating a query yourself, since jquery will take care of a lot of details in the header.

The jjery ajax sample code to call my echo service, which you can configure for your service, is as follows:

  $("#btnEchoString").click(function () { var request = $("#request"); var response = $("#response"); $.ajax({ url: 'EchoService.svc/JSON/Echo', type: "POST", contentType: "application/json; charset=utf-8", data: JSON.stringify({ request: request.val() }), dataType: "json", success: function (data) { response.val(data.d); }, error: function (request, status, error) { //TODO: do something here } }); }); 
+2
source

Source: https://habr.com/ru/post/895030/


All Articles