Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#

Hi All,

i have been banging my head all day but unable to find the reason for this error. I have a WCF REST POST web service. Whenever I am calling the service in fiddler, getting HTTP/1.1 400 Bad Request. I have used Visual Studio's REST template 40(cs) to create this service.

The service looks like
[ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, MaxItemsInObjectGraph = 2147480)]
    public class iHubService
    {
        [WebInvoke(UriTemplate = "LoginUser", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public LogInRes LoginUser(LogInReq objloginReq)
        {
            LogInRes objLoginRes = new LogInRes();
            iHubBusiness.LogIn_BAL objLogIn = new LogIn_BAL();
            try
            {
                objLoginRes = objLogIn.LogInUser(objloginReq);
            }
            catch
            {
            }
        }
    }


The Web.config is
C#
<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding>
          <security mode="Transport" />
        </binding>
      </webHttpBinding>
    </bindings>
    <!--END-->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" defaultOutgoingResponseFormat="Json" maxReceivedMessageSize="1000000"
          maxBufferPoolSize="1000000" maxBufferSize="1000000"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>
</configuration>


When I see the help file for this service in IE, the request JSON format format is
C#
{
	"AppType":"String content",
	"Password":"String content",
	"Username":"String content"
}


Even on passing this default data, I get below error in Fiddler
C#
HTTP/1.1 400 Bad Request
Cache-Control: private
Content-Length: 1790
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 10 Oct 2013 16:07:53 GMT
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Request Error</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id="content">
      <p class="heading1">Request Error</p>
      <p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="https://172.16.3.25/Test/iHubWebService/iHubService/help">service help page</a> for constructing valid requests to the service.</p>
    </div>
  </body>


I have used content-type:application/json in the fiddler.

I have been troubled with this issue the whole day. Is there any way I can check the reason for 40 error. Any suggestion are most welcome.
Posted
Comments
Rasik Banta 10-Oct-13 12:29pm    
*A typo - in the last line - please read 400 error

1 solution

Solution to HTTP 400 Bad Request when POSTing to WCF RESTful web service[^]

EDIT
-------------------
You didn't share the additional error message(400 Bad Request is not an absolute error message). Make sure you're using right content-type & datatype. And use proper exception handling to get more details related to errors. Check these.

WCF REST Based Service Call using JQuery[^]
Effective Error Handling with WCF & REST[^]
WCF and JSON Services[^]
Sending files as a Base64 String to a WCF RESTful Service[^]
WCF POST test harness[^]
WCF REST Exception handling [^]
 
Share this answer
 
v2
Comments
Rasik Banta 10-Oct-13 15:44pm    
@thatraja - I am using JSON and this post is about issue in the xml which was sent in the request. I am passing the exact json as mentioned in the sample request in help page of the service and still getting error so the format of thr request message is not the problem
thatraja 11-Oct-13 6:00am    
Check updated answer
Rasik Banta 11-Oct-13 10:48am    
thanks thatraja for your time in answering my query. I found out the reason for the failure and i feel like punching myself for that :)

In my service logic I was writing logs to a particular directory on my dev server. The server didn't have that directory which was causing fiddler to return Bad Request error. Strangely and much to my relief, everything started working as soon as i created that folder. I dont know why I was getting Bad request error when it was a case of bad implementation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900