Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of using a GET request over a POST request?

Several of my ajax applications in the past have used GET request but now I'm starting to use POST request instead. POST requests seem to be slightly more secure and definitely more url friendly/pretty. Thus, i'm wondering if there is any reason why I should use GET request at all.

like image 912
fuentesjr Avatar asked Oct 12 '08 07:10

fuentesjr


People also ask

Why did we use GET requests rather than POST requests?

Use GET if you want to read data without changing state, and use POST if you want to update state on the server. Show activity on this post. My general rule of thumb is to use Get when you are making requests to the server that aren't going to alter state. Posts are reserved for requests to the server that alter state.

Why is GET better than POST?

GET is less secure than POST because sent data is part of the URL. POST is a little safer than GET because the parameters are stored neither in the browser history nor in the web server logs.

What are advantages of GET?

Advantages of GETThe GET method can retrieve information identified by the request-URl (Uniform Resource Identifier). GET requests can be viewed in the browser history. It enables you to save the results of a HTML form. You can easily use GET method to request required data.


2 Answers

I generally set up the question as thus: Does anything important change after the request? (Logging and the like notwithstanding). If it does, it should be a POST request, if it doesn't, it should be a GET request.

I'm glad that you call POST requests "slightly" more secure, because that's pretty much what they are; it's trivial to fake a POST request by a user to a page. Making it a POST request, however, prevents web accelerators or reloads from re-triggering the action accidentally.

As AJAX, there is one more consideration: if you are returning JSON with callback support, be very careful not to put any sensitive data that you don't want other websites to be able to see in there. Wikipedia had a vulnerability along these lines where the user anti-CSRF token was revealed via their JSON API.

like image 168
Edward Z. Yang Avatar answered Sep 30 '22 15:09

Edward Z. Yang


All good points, however, in answer to the question, GET requests are more useful in certain scenarios over POST requests:

  1. They can be bookmarked
  2. They can be cached
  3. They're faster
  4. They have known consequences (assuming they don't change data), so visiting them multiple times is not a problem.

For the sake of posterity, updating this comment with the blog notes re: point #3 here, all credit to Omar AL Zabir (the author of the referenced blog post):

"Atlas by default makes HTTP POST for all AJAX calls. Http POST is more expensive than Http GET. It transmits more bytes over the wire, thus taking precious network time and it also makes ASP.NET do extra processing on the server end. So, you should use Http Get as much as possible. However, Http Get does not allow you to pass objects as parameters. You can pass numeric, string and date only. When you make a Http Get call, Atlas builds an encoded url and makes a hit to that url. So, you must not pass too much content which makes the url become larger than 2048 chars. As far as I know, that’s what is the max length of any url.

Another evil thing about http post is, it’s actually 2 calls. First browser sends the http post headers and server replies with “HTTP 100 Continue”. When browser receives this, it sends the actual body."

like image 25
jvenema Avatar answered Sep 30 '22 17:09

jvenema