WCF Rest service POST method fails in Fiddler

2019-04-07 21:37发布

问题:

I'm using WCF Restservice to authenticate user. I have a POST method and works fine using jquery but fails on Fiddler test. I get 415 Unsupported Media Type.

Here is my code

[OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "Login", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    public string Login(string userName, string password)
    {
        string valid;

        // Log in user
        int authenticatedId = AuthenticateManager.Authenticate(userName, password);
        if (authenticatedId != -1)
        {
            valid = "Welcome " + userName + "!";
        }
        else
        {
            valid = "Login failed!";
        }

        return valid;
    }

This is what I'm passing in Fiddler body

[{ "userName":"dusshyi", 
   "password":"subram1" }]

This is what fiddler generating

 <HTML><HEAD><STYLE type="text/css">#content{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: #000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white}P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdana}PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e0 1px solid; MARGIN-TOP: -5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2em; PADDING-BOTTOM: 5px; BORDER-LEFT: #f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #f0f0e0 1px solid; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e5e5cc}.heading1{MARGIN-TOP: 0px; PADDING-LEFT: 15px; FONT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTTOM: 3px; MARGIN-LEFT: -30px; WIDTH: 100%; COLOR: #ffffff; PADDING-TOP: 10px; FONT-FAMILY: Tahoma; BACKGROUND-COLOR: #003366}.intro{MARGIN-LEFT: -15px}</STYLE>
<TITLE>Service</TITLE></HEAD><BODY>
<DIV id="content">
<P class="heading1">Service</P>
<BR/>
<P class="intro">Endpoint not found.</P>
</DIV>
</BODY></HTML>

Please help!

回答1:

Have you set the Content-Type of the request to application/json in Fiddler's Request body editor?



回答2:

I had the same problem this morning...

What fixed it for me was to click on the "Composer" tab, then add the following to the "Request Headers" section:

Content-Type: application/x-www-form-urlencoded

Without this line, I noted that the log showed it was attempting to call my REST Service with a Content-Type of "text/html", and the Service wouldn't get called.

This would even happen if I added the line:

Content-Type: application/json

However, after adding the line "Content-Type: application/x-www-form-urlencoded", Fiddler would now call the service with a Content Type of "application/json", and the web service was called correctly. Strange...

I've created a complete walkthough of creating a JSON WCF Web Service, and testing it with Fiddler, on the following page:

http://mikesknowledgebase.com/pages/Services/WebServices-Page1.htm



回答3:

Fiddler Web Debugger:Composer section; settings

Parsed
POST,../Service.svc/Login,HTTP/1.1

Request Header:
User-Agent: Fiddler
Content-Type: application/json;charset=UTF-8
Host: localhost:15021
Content-Length: 43

Request Body:
{"username":"ABC","password":"pwd"}

and then execute