Search code examples
c#web-serviceswcfwebhttp-status-code-405

WCF REST service returns 405 for POST


GET works, but when POST is invoked my service responds with a 405 method not allowed.

 [ServiceContract]
public interface IRestMeraki
{
    [OperationContract]
    [WebInvoke(Method = "OPTIONS", UriTemplate = "")]
    void GetOptions();

    [OperationContract]
    [WebGet(
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "json/")]
    void JSONData();

    [OperationContract]
    [WebInvoke(Method = "POST",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate = "json/{value}")]
    void Post(string value);
 }
}

and my methods (Get options tried after reading this )

public void GetOptions()
    {           
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Content-Type");


    }

        public void JSONData()
    {
       //my code here
    }


    public void Post(string value)
    {
//my code here
    }

I have also added handlers to my web config file

  <handlers>
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <remove name="OPTIONSVerbHandler" />
  <remove name="TRACEVerbHandler" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="POST, GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers> 

I can't change the Uri to use different for each method. I use get for validation and post to receive data. Wireshark showed this 405 error.


Solution

  • You need a * in UriTemplate. I was able to see issue when there is no *.

    UriTemplate = "" //Wrong

    UriTemplate = "*" /Working

    Is the POST working when invoked from same domain?