izengod
izengod

Reputation: 1156

GET vs POST where to use which method

I'm having a little confusion regarding what should be the ideal case for using GET and POST for a Ajax call. Previously when we were using ASP.NET MVC with KnockoutJS, we used to call a custom $.postJson(url,data,callback) from jQuery to our MvcController(even for a GET operation). Recently we are using ASP.NET Web API with AngularJS and using $http(url,method) with GET and passing the search parameters in our url attribute and catching them in ApiController using FromUri attribute.

Now my question is, what should be the ideal scenarios and best practice for using GET & POST (P.S. Do not answer from CRUD operations perspective).

Upvotes: 3

Views: 4249

Answers (2)

Jim L
Jim L

Reputation: 2327

There is a formal definition of what the methods should be doing on the server side: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

Because of that formal definition, any ideal answer inevitably ends up sounding like a CRUD answer. GET should never modify data, it should only retrieve data. POST is intended to change data on the server.

In practice, we often find ourselves using POST as a work around for the fact that GET is effectively limited from having a content body in the request. Many data retrieval scenarios may need to send a content body to the server. You may also run into URL length issues in some GET scenarios that may cause you to send data in POST. So, if you're trying to retrieve data, prefer GET and use POST where you need to.

While I may use POST to work around limitations of GET for data retrieval, I would never use GET to modify data.

Upvotes: 6

kmsj13
kmsj13

Reputation: 28

Use POST if you are going to add & update. Use GET if you are going to fetch.

For Best Practice: for log-in, use POST for submitting credentials which hides the username/password from being sniffed.

Upvotes: -3

Related Questions