What is the difference between GET and POST?

I’ve actually been asked this question in interviews before and knowing when to use either GET or POST is a fundamental tool for web developers.

The difference bewteen GET and POST lies in something called “idempotence” - GET requests are said to be idempotent while POST requests are said to be not idempotent.  If you lookup “idempotence” on wikipedia it says “multiple applications of the operation does not change the result”.  Lets take an example to understand what that means:

Example of idempotence

Think of it this way, when you request a web page from a web server via your browser, sometimes the web page URL contains a few extra variables in the URL after the ? mark, which usually indicates to the web server that you’re looking to pull some dynamic information based on those variables.  Maybe you want to see all results from the month of November, or just results 1 through 10, etc.  This kind of request would be a GET request, because no matter how many times you make the request you are not changing data on the web server (you’re not changing the state of the web server’s data) - so a GET request is said to be idempotent.

Example of non-idempotence

Now, lets say you happen to be signing up for a new user account on your favorite social networking site, in that case you would usually need to fill out a POST form that asks for your name, desired username, password, etc. and then you press “submit”.  By pressing “submit” you are sending data to the web server that will be stored - you’ve just changed the state of the social networking site, and if you submit another user signup request you would be changing the state once again.  So we say that POST requests are not indempotent.

What can you take away from all this?

So if you’re just skimming this article, know that GET requests are idempotent and POST requests are not idempotent.  No matter how many times you perform a GET request you will not change the web server’s data, while performing a POST request more than once may very well change data inside the web server.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.