0

{"The remote server returned an error: (405) Method Not Allowed}System.InvalidOperationException {System.Net.WebException}

Web Config:

      <service name="Service" behaviorConfiguration="RestService">      
        <endpoint address="web" binding="webHttpBinding"
                  contract="IService" behaviorConfiguration="ServiceBehavior">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RestService">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="ServiceBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <!--<protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>-->
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>

Service :

    public test TestPost(test testPost)
    {
        test objtest1 = new test();
        objtest1.Address = "Test";
        objtest1.Name = "Welcome";
        return objtest1;
    }


[DataContract]
public class test
{
    [DataMember(Order = 0)]
    public string Name { get; set; }
    [DataMember(Order = 1)]
    public string Address { get; set; }
}


    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare,
       RequestFormat = WebMessageFormat.Json,
       ResponseFormat = WebMessageFormat.Json,
       UriTemplate = "TestPost/")]
    test TestPost(test i);

Using Fiddler:

POST /RESTfulService/Service.svc/web/TestPost/ HTTP/1.1
User-Agent: Fiddler
Host: localhost:50458
Content-Length: 43

HTTP/1.1 400 Bad Request
Server: ASP.NET Development Server/11.0.0.0
Date: Thu, 19 Dec 2013 07:19:44 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 1647
Cache-Control: private
Content-Type: text/html
Connection: Close

GET Method is working fine, when i am trying to use POST method i am getting error

Patt Mehta
  • 4,110
  • 1
  • 23
  • 47
  • Show the actual XML request you're POSTing... It seems the problem is in the request, as everything else seems fine. – Tisho Dec 19 '13 at 07:41
  • I am using Json format. The format is { "Name" = "test", "Address" = "test" } – user3118128 Dec 19 '13 at 07:45
  • Ops, I missed that. Ok, look at this question: http://stackoverflow.com/questions/575893/why-does-my-c-sharp-client-posting-to-my-wcf-rest-service-return-400-bad-req, seems the same. – Tisho Dec 19 '13 at 07:51
  • 1
    Shouldn't it be `{"Name" : "test", "Address" : "test"}`? i.e. `:` in place of `=`. – shahkalpesh Dec 19 '13 at 07:59
  • By using the above referred link, i used same format of code but cant access that method, please guide me. [OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "TestPost/")] test TestPost(test i); – user3118128 Dec 19 '13 at 08:01
  • used this **{ "Name":"test", "Address":"test" }** but getting error like HTTP/1.1 400 Bad Request – user3118128 Dec 19 '13 at 08:26
  • 1
    Just set the content-type to `application/json`. From the fiddler example given, there is no content-type passed. And this is the problem. – Tisho Dec 19 '13 at 09:22

1 Answers1

0

I have tried above example in my local and found that you are missing one http header called

Content-Type: application/json

Also be sure you pass right json string in request body

{"Name" : "test", "Address" : "test"}

above trick will help you to get rid of 400 bad request.

Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62