从REST客户端发出对WCF服务的请求

时间:2022-06-27 09:49:39

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 * : POST1 and [POST2][2] , but I could not solve the problem. My WCF service code is as follows:

在我当前的项目中,我正在向WCF服务发送POST请求。但是,我得到了空洞的回应。我试图在*上使用类似的帖子:POST1和[POST2] [2],但我无法解决问题。我的WCF服务代码如下:

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:

现在,从REST客户端,我的请求如下:

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

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

Content-Type:application / json和payload是{“firstname”:“Pankesh”}。我没有得到WCF的回复。

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

供大家参考,我附上了客户的截图。

从REST客户端发出对WCF服务的请求

2 个解决方案

#1


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.

一个错误可能是json-String-parameter的不同命名。

EDITS:

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.

另一个可能的错误是你在return语句中使用的变量与你给方法的参数不同。

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

#2


1  

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

这可能是因为您将BodyStyle设置为WebMessageBodyStyle.Wrapped。

When you use Wrapped you should post your data as:

当您使用Wrapped时,您应将您的数据发布为:

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

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

或者,您可以将其更改为WebMessageBodyStyle.Bare,并将其发送为:

{"firstname":"Pankesh"}

#1


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.

一个错误可能是json-String-parameter的不同命名。

EDITS:

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.

另一个可能的错误是你在return语句中使用的变量与你给方法的参数不同。

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

#2


1  

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

这可能是因为您将BodyStyle设置为WebMessageBodyStyle.Wrapped。

When you use Wrapped you should post your data as:

当您使用Wrapped时,您应将您的数据发布为:

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

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

或者,您可以将其更改为WebMessageBodyStyle.Bare,并将其发送为:

{"firstname":"Pankesh"}