Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of WebInvoke POST compared to WEBGET

Tags:

rest

wcf

hi i found one of the examples of wcf REST with a WEBINVOKE method just like the following

[OperationContract] [WebInvoke( BodyStyle=WebMessageBodyStyle.Bare, Method="POST", RequestFormat=WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml, UriTemplate="CreateStudent/{StudentName}/{Chair}/{AverageNote}")] int Insert(string StudentName, string Chair, string AverageNote);   [OperationContract] [WebGet( BodyStyle= WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)] Student[] GetAllStudents(); 

my question is can i use WEBGET method instead of WEBINVOKE just like below and what exactly is the difference betwenn WEBINVOKE POST and WEBGET, as per my observation we are sending the parameters by appending query strings in the URI Templates for both WEbGet and WebInvoke POST, what are the advantages that we can get using WebInvoke POST which we can not get using WEBGET

[OperationContract] [WebGet( BodyStyle=WebMessageBodyStyle.Bare,  RequestFormat=WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml, UriTemplate="CreateStudent/{StudentName}/{Chair}/{AverageNote}")] int Insert(string StudentName, string Chair, string AverageNote); 
like image 302
venkat Avatar asked May 26 '11 09:05

venkat


People also ask

What is the difference between WebGet and WebInvoke?

The main difference between WebGet and WebInvoke is that WebGet is used to retrieve data while WebInvoke is used to update data. WCF stands for Windows Communication Foundation developed by Microsoft. It is used to develop service-oriented applications.

What is WebInvoke?

WebInvoke (Commonly used for data input/update) The WebInvoke attribute exposes services using other HTTP verbs such as POST, PUT, and DELETE. POST is the default value but it can be changed by setting the Method property of the attribute. The WebInvoke attribute should be used only for data input/update.


2 Answers

It is very big difference. First of all REST is usually used with these HTTP verbs:

  • GET - retrieving items
  • POST - inserting items
  • PUT - updating items
  • DELETE - deleting items

You should never use GET for anything else then retrieving items. Using HTTP GET for data modification is considered as a bad practice in whole web development. To trigger GET you just need to create link on the web page or simply type a URL to the browser. You will hit refresh 50 times and you have 50 same inserts. Data modification should be always done with POST. If you have form which triggers HTTP POST (Post cannot be triggered directly) and you hit refresh browser will usually ask you if you want the form to be submitted again = if you really want to post and process the data again to the server.

Another problem is that GET request can be cached and redirected but POST requests cannot.

like image 87
Ladislav Mrnka Avatar answered Sep 18 '22 11:09

Ladislav Mrnka


This link should provide further insight into the answers provided:

http://blog.markkoltnuk.com/2011/02/14/understanding-wcf-webinvokewebget-attributes/

Lets explain, once and for all, what the difference between WebInvoke & WebGet.

WebGet (Commonly used to retrieve data)

The WebGet attribute exposes operations using the GET verb. You can access the endpoint is directly via a Web browser by typing the URI to the service into the address bar. Parameters can be sent within the URI either as query string parameters or embedded in the URI. The WebGet attribute should be used only for data retrieval due to its caching capabilities.

WebInvoke (Commonly used for data input/update) The WebInvoke attribute exposes services using other HTTP verbs such as POST, PUT, and DELETE. POST is the default value but it can be changed by setting the Method property of the attribute. The WebInvoke attribute should be used only for data input/update.

like image 36
ramizmoh Avatar answered Sep 21 '22 11:09

ramizmoh