Can't Pass JSON Post Data to WCF REST Service using Fiddler

自古美人都是妖i 提交于 2019-12-07 16:39:20

问题


I'm trying to invoke a WCF rest service as shown below:

[WebInvoke(UriTemplate = "Login", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public string Process(string AuthenticationInfo)
{

I'm trying to invoke it using the following below in Fiddler 2:

User-Agent: Fiddler
Host: localhost
content-type: application/json;charset=utf-8
content-length: 0
data: {"AuthenticationInfo": "data"}

I have a breakpoint in the method, and it does hit the breakpoint, but the value for AuthenticationInfo is always null, and not "data".

What am I doing wrong?

Thanks.


回答1:


The default "body style" of the [WebInvoke] attribute is "Bare", which means that the input (in your case, "data") must be sent "as is". What you're sending is a wrapped version of the input (i.e., wrapped in an object whose key is the parameter name.

There are two ways you can make this work: either change the WebInvoke declaration to include the BodyStyle parameter:

[WebInvoke(
    UriTemplate = "Login", 
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public string Process(string AuthenticationInfo)   

Or you can change the request to send the parameter "bare":

POST .../Login HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Type: application/json;charset=utf-8
Content-Length: 6

"data"



回答2:


Are you setting the HTTP method to POST in Fiddler? By default WCF REST operations are POST, if you want to do gets you need to set Method="GET" on the WebInvoke attribute. BTW, in your case I don't think a GET makes sense since you're sending in data, so just make sure you're using POST in Fiddler.




回答3:


You send Content-Length as 0, but it should be the length of your POST-Data.



来源:https://stackoverflow.com/questions/10290210/cant-pass-json-post-data-to-wcf-rest-service-using-fiddler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!