0

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

user-517752
  • 1,188
  • 5
  • 21
  • 54
  • 1
    You seem to be getting a 500. The service cannot be activated. Browse to http://localhost/RestfulWCFService/RestfulTestService.svc and see why. – smoksnes Aug 04 '16 at 11:58
  • @smoksnes : the service is already activated when I make a GET request. I am updating contract defintions and its implementation file for your reference. – user-517752 Aug 04 '16 at 12:30
  • Ok, so the status message you're showing in the client is not related to the request you're making? – smoksnes Aug 04 '16 at 12:37
  • yes, the screenshot is about the POST request, it is not for GET request. I am just making the GET request to check that the service is activated or not ... – user-517752 Aug 04 '16 at 12:39
  • @smoksnes: i have made edits based on your and Robin suggestions. Now, I am getting response, but with a null string. Request to provide suggestions. – user-517752 Aug 04 '16 at 13:04
  • It's probably because your `WebMessageBodyStyle`. I've added an answer with more info. – smoksnes Aug 04 '16 at 13:15

2 Answers2

1

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; 
}
rbr94
  • 2,227
  • 3
  • 23
  • 39
  • 1
    Aha, Good catch!!! I have updated. But still the same problem. Let me update this in the question in order to avoid confusion. – user-517752 Aug 04 '16 at 12:37
  • I'm also a bit confused about your content-length being "0". Did you try to set "Content-Length" to the length of your json-string as header parameter? – rbr94 Aug 04 '16 at 12:40
  • I have not set explicitly `content-length` as 0. Do you want me to use other client to test the POST request??? – user-517752 Aug 04 '16 at 12:46
  • I have made a re-request to the server from REST client, Now, the content-length is not ZERO. It is 25. – user-517752 Aug 04 '16 at 12:48
  • Firstly try to set `content-length` in the raw headers in the tool you use above. If this does not solve your problem, try to write a short PHP Script to call your service. In this post there's is a short example for that: http://stackoverflow.com/questions/10703060/how-to-call-restful-wcf-service-from-php – rbr94 Aug 04 '16 at 12:50
  • Thanks to Robin your pointers. I have made corrections as you were advising. :) : ) Now, I am getting response. But, just hello is printed with null string. I am updating the screenshot for further suggestions. – user-517752 Aug 04 '16 at 12:56
  • Many thanks for the previous corrections and Suggestions! I have updated the screenshot with new results. Kindly provide suggestions! – user-517752 Aug 04 '16 at 13:01
  • I've edited my post. If that solved your problem, please mark my answer as correct. – rbr94 Aug 04 '16 at 13:07
  • it was just a typo in the question. In my implementation, the variable request has already been corrected. But, I am still getting the same response. – user-517752 Aug 04 '16 at 13:10
1

It's probably because you set BodyStyle to WebMessageBodyStyle.Wrapped.

When you use Wrapped you should post your data as:

{"jsonRequestString":{"firstname":"Pankesh"}}

Alternatively you can change it to WebMessageBodyStyle.Bare, and send it as:

{"firstname":"Pankesh"}
smoksnes
  • 10,509
  • 4
  • 49
  • 74
  • Thanks! When I pass the {"jsonRequestString":"Pankesh"}. It returns "Hello Pankesh" fine. But, what if I want to pass the multiple JSON data. I mean - I want to pass {"firstname":"Pankesh"}{"lastname":"Patel"} in the body without changing the Interface definition. Is it possible through this? – user-517752 Aug 04 '16 at 16:13
  • I see that an answer was accepted, so I assume you solved it? – smoksnes Aug 04 '16 at 16:26
  • For one parameters in the body, it is solved. But, still the question/challenge remain unsolved -- how I can pass multiple JSON objects in the body??? – user-517752 Aug 04 '16 at 16:28