Search code examples
c#angularjswcfrestangular

Post Method Not Allowed


I tried this example: http://www.c-sharpcorner.com/UploadFile/surya_bg2000/developing-wcf-restful-services-with-get-and-post-methods/.

The GET Method works perfect, but not the POST method. When debugging the strReturnValue variable is always empty. When I continue the status is: 405 Method Not Allowed. What am I doing wrong?

In C# I had to change the Method from POST to OPTIONS.

I am using Restangular (angular js). Here is the frontend function:

        var message = {
            Name: new_player.name,
            Created: (new Date()).toJSON(),
            Affilation: new_player.human,
            auth: new_player.auth
        }
        return Restangular.one('').post('CreatePlayer', message).then(function(){
            console.log("Object saved OK");
          }, function() {
            console.log("There was an error saving");               
        });

enter image description here

Edit

[System.ServiceModel.OperationContract]
    [System.ServiceModel.Web.WebInvoke(UriTemplate = "CreatePlayer", Method = "OPTIONS", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string CreatePlayer(System.IO.Stream data);

    public string CreatePlayer(System.IO.Stream data) {

        //convert stream data to StreamReader
        System.IO.StreamReader reader = new System.IO.StreamReader(data);

        //read StreamReader data as string
        string XML_string = reader.ReadToEnd();
        string result = XML_string;

        //return the XMLString data
        return result;
    }

Solution

  • Had to change the header of my frontend post-call to: 'application/x-www-form-urlencoded' (the default Internet media type).

            return Restangular.one('').customPOST({
                Name: new_player.name,
                Created: new Date(),
                Affilation: new_player.human,
                auth: new_player.fed
                }, 'CreatePlayer', {},
                {'Content-Type': 'application/x-www-form-urlencoded'
            })
    

    Alternatively you could use the angular $http service:

    $http({
        url: 'http://localhost:31736/BusinessService.svc/CreatePlayer',
        method: 'POST', 
        data: "test",
        headers: {"Content-Type": "application/x-www-form-urlencoded"}
    }); 
    

    That was it! Then I could serialize the resulting string and move forward to my Business Logic.

        public string CreatePlayer(System.IO.Stream data) {
    
            //convert stream data to StreamReader
            System.IO.StreamReader reader = new System.IO.StreamReader(data);
    
            //read StreamReader data as string
            string XML_string = reader.ReadToEnd();
    
            System.Web.Script.Serialization.JavaScriptSerializer json_serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            BusinessObjects.Player Player = json_serializer.Deserialize<BusinessObjects.Player>(XML_string);
    
            return BL_CreatePlayer.CreatePlayer(Player);
        }