Pankesh Pankesh - 4 months ago 24
JSON Question

Post Request to WCF service from a REST client

In my current project, I am sending a POST request to a WCF service. But, I am getting empty response. I have tried to use the similar post on stackoverflow : POST1 and [POST2][2] , but I could not solve the problem.
My WCF service code is as follows:

namespace RestfulWCFService
{

[OperationContract]
[WebInvoke(Method="GET", ResponseFormat= WebMessageFormat.Xml, UriTemplate="xml/?firstname={firstname}&lastname={lastname}")]
string SayHelloXml(string firstname, string lastname);

[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "json/{name}")]
string SayHelloJson(string name);



[ServiceContract]
public interface IRestfulTestService
{

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/data")]
string SayHelloJSONPOSTRequest(string jsonRequestString);

}
}


The implementation of the interface is as follows:

namespace RestfulWCFService
{

string IRestfulTestService.SayHelloXml(string firstname, string lastname)
{
return "Hello " + firstname + " " + lastname;
}

string IRestfulTestService.SayHelloJson(string name)
{
return "Hello " + name;
}


public class RestfulTestService : IRestfulTestService
{
string IRestfulTestService.SayHelloJSONPOSTRequest(string jsonRequestString)
{
return "Hello " + jsonRequestString;
}

}
}


Now, from a REST client, my request is as follow:

http://localhost/RestfulWCFService/RestfulTestService.svc/data


and the
Content-Type:application/json
and payload is
{"firstname":"Pankesh"}
. I am getting no response from WCF.

For your reference, I am attaching the screenshot of my client.

enter image description here

Answer

In the interface you use:

string SayHelloJSONPOSTRequest(string jsonRequestString);

But in the implementation of the interface you use:

string IRestfulTestService.SayHelloJSONPOSTRequest(string jsonString)

One mistake could be the different naming of the json-String-parameter.

EDITS:

Firstly try to set content-length in the raw headers in the tool you use above.

Another possible mistake is tha the variable you use in the return statement is not the same as the parameter you given to the method.

string IRestfulTestService.SayHelloJSONPOSTRequest(string jsonRequestString)
{
     return "Hello  " + jsonString; 
}