none
The difference between WCF RESTFUL Service Method names? RRS feed

  • Question

  • Now i just start to learn the restful WCF service. There are four method GET POST PUT DELETE.

    like this

          [OperationContract]
            [WebInvoke(UriTemplate = "Client/Add", Method = "POST", RequestFormat = WebMessageFormat.Json, 
             ResponseFormat = WebMessageFormat.Json)]
            string Create_Client(CLIENT postClient);

    I had done a test. When delete  and update the entity Client I don't use the PUT and DELETE method as method name but use POST. It also works .

     [OperationContract]
            [WebGet(UriTemplate = "Client/Get", ResponseFormat = WebMessageFormat.Json)]
            List<CLIENT> GetCollection_Client();
    
            [OperationContract]
            [WebInvoke(UriTemplate = "Client/Add", Method = "POST", RequestFormat = WebMessageFormat.Json, 
             ResponseFormat = WebMessageFormat.Json)]
            string Create_Client(CLIENT postClient);
    
            [OperationContract]
            [WebInvoke(UriTemplate = "Client/Delete", Method = "POST", 
             ResponseFormat = WebMessageFormat.Json)]
            string Delete_Client(int id);
    
            [OperationContract]
            [WebInvoke(UriTemplate = "Client/Update", Method = "POST", RequestFormat = WebMessageFormat.Json,
             ResponseFormat = WebMessageFormat.Json)]
            string Update_Client(CLIENT postClient);

    So what's the difference between (PUT,DELETE) and POST for Detete or Modify the entity.

    I am so confused!.


    One day a small demo!

    Wednesday, January 9, 2013 6:20 AM

Answers

  • Hi,

    >>I had done a test. When delete  and update the entity Client I don't use the PUT and DELETE method as method name but use POST. It also works .

    Yes, you can use HTTP POST to update or delete data, but it is not a suggested behavior. The common senerio is that you use the HTTP POST request to create a new item operations, using HTTP PUT request to update the operation of the existing data, using the HTTP DELETE request to define the delete operation.

    For HTTP protocol, POST request is non-peer-to-peer, but PUT request and DELETE request is peer-to-peer. It means that PUT request and DELETE requests can invoke operations repeatedly, those operations have the same effect: they appear to only be executed once; This is not the same for POST, for example using a POST request to add the same information repeatedly may cause the duplication of data.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 10, 2013 7:45 AM
    Moderator
  • Hi,

    It make no difference using POST or DELETE to delete an item. As I said above, it may result in the duplication of data when add an item with POST repeatedly.

    The benefits of POST and DELETE is they are idempotent(peer-to-peer). I'd suggest you take a look at a post below:

    http://jcalcote.wordpress.com/2008/10/16/put-or-post-the-rest-of-the-story/

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 16, 2013 7:15 AM
    Moderator

All replies

  • Hi,

    >>I had done a test. When delete  and update the entity Client I don't use the PUT and DELETE method as method name but use POST. It also works .

    Yes, you can use HTTP POST to update or delete data, but it is not a suggested behavior. The common senerio is that you use the HTTP POST request to create a new item operations, using HTTP PUT request to update the operation of the existing data, using the HTTP DELETE request to define the delete operation.

    For HTTP protocol, POST request is non-peer-to-peer, but PUT request and DELETE request is peer-to-peer. It means that PUT request and DELETE requests can invoke operations repeatedly, those operations have the same effect: they appear to only be executed once; This is not the same for POST, for example using a POST request to add the same information repeatedly may cause the duplication of data.

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 10, 2013 7:45 AM
    Moderator
  • hi, Haixia Xie

    Thanks for your reply. 

    The content your reply is almost like the ones I get from the internet. The key of the question is what is the side effect of using POST instead of DELETE and PUT. From the code I offered, I use the POST method to finish the deletion. If I execute this multiple times , it will not change anything. Because it will map to a method which defined in the service class. And this method will delete only the record with fixed id just like the DELETE action. The put is same as this one.  So could you tell me what's exactly the difference between POST and (DELETE PUT) essentially? If I use POST instead of DELETE and PUT what is the result ? 

    Thanks you for helping me. Really appreciate!


    One day a small demo!

    Thursday, January 10, 2013 9:43 AM
  • Hi,

    It make no difference using POST or DELETE to delete an item. As I said above, it may result in the duplication of data when add an item with POST repeatedly.

    The benefits of POST and DELETE is they are idempotent(peer-to-peer). I'd suggest you take a look at a post below:

    http://jcalcote.wordpress.com/2008/10/16/put-or-post-the-rest-of-the-story/

    Best Regards.


    Haixia
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 16, 2013 7:15 AM
    Moderator