• You get a HTTP 400 Bad Request when POSTing to a WCF service

    by  • February 1, 2012 • .net, wcf • 4 Comments

    I was recently debugging an issue with a WCF service I had written, where it appeared that POSTing data to the service returned a HTTP 400 Bad Request error. There are several reasons you may experience such an error but mine was peculiar in that it wasn’t always repeatable and depended on what the client application sent to the service.

    On closer investigation, it turned out I was hitting the default maxReceivedMessageSize (set to 65536 bytes) when POSTing the data on the basicHttpBinding binding I was using, as shown on the Fiddler trace above (highlighted in red) — but only just.

    How to get around it.

    The WCF service was defined by an endpoint using the basicHttpBinding binding, so it was a case of adding the following:

    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="" maxReceivedMessageSize="262144"></binding>
          </basicHttpBinding>
        </bindings>
        ...
    </system.serviceModel>

    Here I am setting the default basicHttpBinding binding to accept messages up to 256KB in size. This immediately fixed the problem. This comes with a caveat though as setting this value too high on production code could increase your exposure to possible DoS attacks from large payloads.

    About

    .NET developer at thetrainline.com, previously web developer at MRM Meteorite. Awarded a PhD in misbehaviour detection in wireless ad-hoc networks.A keen C# ASP.net developer bridging the gap with APIs and JavaScript frameworks, one web app at a time.

    http://www.paulkiddie.com

    4 Responses to You get a HTTP 400 Bad Request when POSTing to a WCF service

    1. tom mcd
      July 14, 2012 at 3:07 am

      You are a genius. I’ve been trying to fix this for days.

    2. Eric
      July 23, 2012 at 8:53 pm

      Thanks it worked for me.

    3. :O
      August 20, 2013 at 3:31 pm

      Ironically I set this, continued. Bug came back. spent couple of days trying to figure it out.

      Then I realised, after seeing this that maybe I hadnt set it big enough, so it overflows in certain situations still.

      (facepalm)

      Thanks! Saved me combusting.

    4. Yogesh
      September 7, 2015 at 5:44 am

      Unable to solve error still showing error with Post “my post method link” 400 bad request.

    Leave a Reply

    Your email address will not be published. Required fields are marked *