title: WCF Data Service returns 400 Bad Request for POST queries
body: <p>I need to host a WCF Data service as part of a legacy ASP.NET MVC website running in IIS 7 Integrated mode.</p>
  
  <p>Read access to the web service is working fine, but whenever I try to POST to the web service, I get a 400 Bad Request error.</p>
  
  <p>For testing purposes, I have downloaded the sample odata service from <a href="http://www.odata.org/developers/odata-sdk#/media/7582/odatasampleservices.zip" rel="nofollow noreferrer">http://www.odata.org/developers/odata-sdk#/media/7582/odatasampleservices.zip</a>. Posting to that webservice  works fine when running it in a separate IIS website. However, when I put it in my legacy website, I am geting that 400 Bad Request error wen sending a post request to the service again.</p>
  
  <p>Therefore the problem seems to be related to the configuration of my ASP.NET MVC project. However, I am at a loss on how to proceed.</p>
  
  <p>This is the web config file I am using in my legacy project:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!-- 
   Note: As an alternative to hand editing this file you can use the 
   web admin tool to configure settings for your application. Use
   the Website-&gt;Asp.Net Configuration option in Visual Studio.
   A full list of settings and comments can be found in 
   machine.config.comments usually located in 
   \Windows\Microsoft.Net\Framework\v2.x\Config 
  --&gt;
  &lt;configuration&gt;
   &lt;configSections&gt;
   &lt;section name="combres" type="Combres.ConfigSectionSetting, Combres" /&gt;
   &lt;section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" /&gt;
   &lt;sectionGroup name="elmah"&gt;
    &lt;section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" /&gt;
    &lt;section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" /&gt;
    &lt;section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" /&gt;
    &lt;section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" /&gt;
    &lt;section name="errorMailSAZ" requirePermission="false" type="ElmahFiddler.ElmahMailSAZSectionHandler, ElmahFiddler" /&gt;
   &lt;/sectionGroup&gt;
   &lt;/configSections&gt;
  
   &lt;combres definitionUrl="~/App_Data/combres.xml" /&gt;
   &lt;log4net&gt;
   &lt;root&gt;
    &lt;level value="ALL" /&gt;
    &lt;appender-ref ref="RollingFile" /&gt;
   &lt;/root&gt;
   &lt;logger name="Combres"&gt;
    &lt;level value="DEBUG" /&gt;
   &lt;/logger&gt;
   &lt;appender name="RollingFile" type="log4net.Appender.RollingFileAppender"&gt;
    &lt;file value="log.txt" /&gt;
    &lt;appendToFile value="true" /&gt;
    &lt;maximumFileSize value="100KB" /&gt;
    &lt;maxSizeRollBackups value="2" /&gt;
    &lt;layout type="log4net.Layout.PatternLayout"&gt;
    &lt;conversionPattern value="%d [%t] %-5p %c - %m%n" /&gt;
    &lt;/layout&gt;
   &lt;/appender&gt;
   &lt;/log4net&gt;
   &lt;appSettings configSource="appSettings.config" /&gt;
   &lt;connectionStrings configSource="connectionstrings.config"&gt;
  
   &lt;/connectionStrings&gt;
   &lt;!-- Mail server settings--&gt;
   &lt;system.net&gt;
      &lt;mailSettings/&gt;
   &lt;/system.net&gt;
   &lt;system.web&gt;
   &lt;globalization uiCulture="de" culture="de-DE" /&gt;
  
   &lt;compilation debug="true"&gt;
    &lt;assemblies&gt;
    &lt;add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
    &lt;add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /&gt;
    &lt;add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" /&gt;
    &lt;add assembly="System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
    &lt;add assembly="System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
    &lt;add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /&gt;
    &lt;add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /&gt;
    &lt;add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /&gt;
    &lt;add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
    &lt;add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /&gt;
    &lt;add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /&gt;
    &lt;/assemblies&gt;
    &lt;buildProviders&gt;
    &lt;add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;
    &lt;/buildProviders&gt;
   &lt;/compilation&gt;
  
   &lt;authentication mode="Forms"&gt;
    &lt;!-- User muessen sich nach 30 Tagen abwesenheit neu einloggen (wenn remember me angeklickt wird)--&gt;
    &lt;forms loginUrl="~/Profile/LogOn" timeout="43200" slidingExpiration="true" /&gt;
   &lt;/authentication&gt;
   &lt;membership&gt;
    &lt;providers&gt;
    &lt;clear /&gt;
    &lt;add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="500" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" /&gt;
    &lt;/providers&gt;
   &lt;/membership&gt;
   &lt;profile&gt;
    &lt;providers&gt;
    &lt;clear /&gt;
    &lt;add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" /&gt;
    &lt;/providers&gt;
   &lt;/profile&gt;
   &lt;roleManager enabled="true"&gt;
    &lt;providers&gt;
    &lt;clear /&gt;
    &lt;add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;
    &lt;add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;
    &lt;/providers&gt;
   &lt;/roleManager&gt;
   &lt;customErrors configSource="customErrors.config" /&gt;
   &lt;pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"&gt;
    &lt;namespaces&gt;
    &lt;add namespace="System.Web.Mvc" /&gt;
    &lt;add namespace="System.Web.Mvc.Ajax" /&gt;
    &lt;add namespace="System.Web.Mvc.Html" /&gt;
    &lt;add namespace="System.Web.Routing" /&gt;
    &lt;add namespace="System.Linq" /&gt;
    &lt;add namespace="System.Collections.Generic" /&gt;
    &lt;add namespace="xVal.Html" /&gt;
    &lt;add namespace="Zeiterfassung.Views" /&gt;
    &lt;add namespace="Zeiterfassung.Models" /&gt;
    &lt;add namespace="Zeiterfassung" /&gt;
    &lt;add namespace="GrigoreComponents.Generic" /&gt;
    &lt;add namespace="DeverMind.Generic" /&gt;
    &lt;!--&lt;add namespace="MvcContrib.UI.Grid.ActionSyntax" /&gt;--&gt;
    &lt;/namespaces&gt;
   &lt;/pages&gt;
   &lt;httpHandlers&gt;
    &lt;add path="captcha.ashx" verb="GET" type="ManagedFusion.Web.Mvc.Handlers.CaptchaImageHandler, ManagedFusion, Version=1.0.3490.29346, Culture=neutral" validate="false" /&gt;
    &lt;add path="*.mvc" verb="*" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" validate="false" /&gt;
    &lt;!--&lt;add path="VSEnterpriseHelper.axd" verb="GET" type="Microsoft.VisualStudio.Enterprise.Common.AspNetHelperHandler, Microsoft.VisualStudio.Enterprise.ASPNetHelper, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;--&gt;
    &lt;add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" /&gt;
    &lt;add path="/elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" /&gt;
   &lt;/httpHandlers&gt;
   &lt;httpModules&gt;
    &lt;add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" /&gt;
   &lt;/httpModules&gt;
   &lt;/system.web&gt;
   &lt;system.web.extensions&gt;
   &lt;scripting&gt;
    &lt;webServices&gt;
    &lt;authenticationService enabled="true" requireSSL="false" /&gt;
    &lt;/webServices&gt;
   &lt;/scripting&gt;
   &lt;/system.web.extensions&gt;
   &lt;!-- 
    The system.webServer section is required for running ASP.NET AJAX under Internet
    Information Services 7.0. It is not necessary for previous version of IIS.
   --&gt;
   &lt;system.webServer&gt;
  
   &lt;validation validateIntegratedModeConfiguration="false" /&gt;
   &lt;modules runAllManagedModulesForAllRequests="true"&gt;
    &lt;add name="Elmah.ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" /&gt;
    &lt;add name="Elmah.ErrorFilter" type="Elmah.ErrorFilterModule" preCondition="managedHandler" /&gt;
    &lt;add name="Elmah.ErrorMail" type="Elmah.ErrorMailModule" preCondition="managedHandler" /&gt;
    &lt;add name="elmahSAZ" type="ElmahFiddler.ElmahMailSAZTraceModule, ElmahFiddler" /&gt;
   &lt;/modules&gt;
   &lt;handlers&gt;
    &lt;add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" /&gt;
    &lt;remove name="MvcHttpHandler" /&gt;
    &lt;remove name="UrlRoutingHandler" /&gt;
    &lt;add name="MvcHttpHandler" preCondition="integratedMode" verb="*" path="*.mvc" type="System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt;
    &lt;!--&lt;add name="VSEnterpriseHelper.axd" verb="GET" path="VSEnterpriseHelper.axd" preCondition="integratedMode" type="Microsoft.VisualStudio.Enterprise.Common.AspNetHelperHandler, Microsoft.VisualStudio.Enterprise.ASPNetHelper, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/&gt;--&gt;
    &lt;add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /&gt;
   &lt;/handlers&gt;
   &lt;!--404 fehler sind was besonderes, ich will sie nicht in elmah sehen und ausserdem funktioniert es aus unbekannten gruenden eh nicht wenn ich sie auf dem hetzner-server per 
   customerrors handle--&gt;
   &lt;httpErrors&gt;
    &lt;remove statusCode="404" subStatusCode="-1" /&gt;
    &lt;error statusCode="404" prefixLanguageFilePath="" path="/Error/FileNotFound" responseMode="ExecuteURL" /&gt;
   &lt;/httpErrors&gt;
   &lt;staticContent&gt;
    &lt;!--cache static content on the client side for 7 days--&gt;
    &lt;clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /&gt;
   &lt;/staticContent&gt;
   &lt;rewrite&gt;
    &lt;rules&gt;
    &lt;rule name="CanonicalHostName-Adrian" enabled="true" stopProcessing="true"&gt;
     &lt;match url="(.*)" /&gt;
     &lt;conditions&gt;
     &lt;add input="{HTTP_HOST}" pattern="^worklogger\.de:4567$" /&gt;
     &lt;/conditions&gt;
     &lt;action type="Redirect" url="http://www.worklogger.de:4567/{R:1}" /&gt;
    &lt;/rule&gt;
    &lt;rule name="CanonicalHostName-Sandra" enabled="true" stopProcessing="true"&gt;
     &lt;match url="(.*)" /&gt;
     &lt;conditions&gt;
     &lt;add input="{HTTP_HOST}" pattern="^worklogger\.de:7654$" /&gt;
     &lt;/conditions&gt;
     &lt;action type="Redirect" url="http://www.worklogger.de:7654/{R:1}" /&gt;
    &lt;/rule&gt;
    &lt;rule name="CanonicalHostName-Production" enabled="true" stopProcessing="true"&gt;
     &lt;match url="(.*)" /&gt;
     &lt;conditions&gt;
     &lt;add input="{HTTP_HOST}" pattern="^logmytime\.de$" /&gt;
     &lt;/conditions&gt;
     &lt;action type="Redirect" url="http://www.logmytime.de/{R:1}" /&gt;
    &lt;/rule&gt;
    &lt;rule name="RemoveTrailingSlashRule1" enabled="true" stopProcessing="true"&gt;
     &lt;match url="(.*)/$" /&gt;
     &lt;conditions&gt;
     &lt;add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /&gt;
     &lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt;
     &lt;/conditions&gt;
     &lt;action type="Redirect" url="{R:1}" /&gt;
    &lt;/rule&gt;
    &lt;rule name="Rewrite rule for RewriteMap" enabled="true" stopProcessing="true"&gt;
     &lt;match url=".*" /&gt;
     &lt;conditions&gt;
     &lt;add input="{RewriteMap:{REQUEST_URI}}" pattern="(.+)" /&gt;
     &lt;/conditions&gt;
     &lt;action type="Redirect" url="{C:1}" appendQueryString="false" /&gt;
    &lt;/rule&gt;
    &lt;rule name="jquery-UI images" enabled="true" stopProcessing="true"&gt;
     &lt;match url="(.*)combres.axd/UserAreaCSS/images/(.*)" /&gt;
     &lt;conditions logicalGrouping="MatchAny"&gt;&lt;/conditions&gt;
     &lt;action type="Rewrite" url="Scripts/jquery-ui/css/custom-theme/images/{R:2}" /&gt;
    &lt;/rule&gt;
      &lt;rule name="Add API trailing slash without redirecting the user to a different domain" enabled="false" patternSyntax="ExactMatch" stopProcessing="true"&gt;
       &lt;match url="API/V1/APi.svc" /&gt;
       &lt;action type="Redirect" url="API/V1/APi.svc/" redirectType="Permanent" /&gt;
      &lt;/rule&gt;
    &lt;/rules&gt;
    &lt;rewriteMaps&gt;
    &lt;rewriteMap name="RewriteMap"&gt;
  
     &lt;add key="/Zeiterfassung/zeiterfassungssoftware.html" value="/Zeiterfassung/Arbeitszeit/Zeiterfassungssoftware" /&gt;
    &lt;/rewriteMap&gt;
    &lt;/rewriteMaps&gt;
   &lt;/rewrite&gt;
   &lt;/system.webServer&gt;
   &lt;runtime&gt;
   &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
    &lt;dependentAssembly&gt;
    &lt;assemblyIdentity name="Microsoft.VisualStudio.Enterprise.ASPNetHelper" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /&gt;
    &lt;codeBase version="9.0.0.0" href="file:///C:/Program%20Files%20(x86)/Microsoft%20Visual%20Studio%209.0/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.Enterprise.ASPNetHelper.DLL" /&gt;
    &lt;/dependentAssembly&gt;
    &lt;dependentAssembly&gt;
    &lt;assemblyIdentity name="Microsoft.VisualStudio.Enterprise.ASPNetHelper" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /&gt;
    &lt;codeBase version="9.0.0.0" href="file:///C:/Program%20Files%20(x86)/Microsoft%20Visual%20Studio%209.0/Common7/IDE/PrivateAssemblies/Microsoft.VisualStudio.Enterprise.ASPNetHelper.DLL" /&gt;
    &lt;/dependentAssembly&gt;
    &lt;dependentAssembly xmlns=""&gt;
    &lt;assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /&gt;
    &lt;bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" /&gt;
    &lt;/dependentAssembly&gt;
   &lt;/assemblyBinding&gt;
   &lt;/runtime&gt;
   &lt;location path="VSEnterpriseHelper.axd"&gt;
   &lt;system.web&gt;
    &lt;authorization&gt;
    &lt;allow users="?" /&gt;
    &lt;/authorization&gt;
   &lt;/system.web&gt;
   &lt;/location&gt;
   &lt;!-- Deny everyone except siteadmins to see the elmah.axd --&gt;
   &lt;location path="elmah.axd"&gt;
   &lt;system.web&gt;
    &lt;authorization&gt;
    &lt;allow roles="SiteAdmin" /&gt;
    &lt;deny users="*" /&gt;
    &lt;/authorization&gt;
   &lt;/system.web&gt;
   &lt;/location&gt;
   &lt;system.serviceModel&gt;
   &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" /&gt;
   &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Thanks for your help,</p>
  
  <p>Adrian</p>
  
  <p><strong>Edit:</strong> Not sure if it's any helpe, but here's the fiddler output and the failed request trace:</p>
  
  <pre>
  > curl -u login:pass
  > "http://www.worklogger.de:4567/Testapi/Testapi.svc/Projects"
  > -v -d test
  > * About to connect() to www.worklogger.de port 4567 (#0)
  > *   Trying 192.168.0.109... connected
  > * Connected to www.worklogger.de (192.168.0.109) port 4567 (#0)
  > * Server auth using Basic with user 'login'
  > POST /Testapi/Testapi.svc/Projects HTTP/1.1
  > Authorization: Basic bG9naW46cGFzcw==
  > User-Agent: curl/7.19.0 (i686-suse-linux-gnu) libcurl/7.19.0 OpenSSL/0.9.8h zlib/1.2.3 libidn/1.10
  > Host: www.worklogger.de:4567
  > Accept: */*
  > Content-Length: 4
  > Content-Type: application/x-www-form-urlencoded
  >
  &lt; HTTP/1.1 400 Bad Request
  &lt; Cache-Control: private
  &lt; Server: Microsoft-IIS/7.5
  &lt; X-AspNet-Version: 4.0.30319
  &lt; Set-Cookie: ASP.NET_SessionId=arrujp3wazcugm55v4ysyuew; path=/; HttpOnly
  &lt; X-Powered-By: ASP.NET
  &lt; Date: Sun, 27 Jun 2010 13:24:05 GMT
  &lt; Content-Length: 0
  &lt;
  * Connection #0 to host www.worklogger.de left intact
  * Closing connection #0
  </pre>
  
  <p>I saved the failed request trace to pastebin to <a href="http://pastebin.com/K3uEZfVS" rel="nofollow noreferrer">http://pastebin.com/K3uEZfVS</a> since StackOverflow does not allow posts to grow over 50k.</p>
  
  <p><strong>Edit2:</strong> I would like to point out again that the very same web service runs fine (both POST and GET!) when putting it in a brandnew ASP.NET MVC website with no other changes. Therefore the problem cannot be related to the way I am making the request.</p>