Get request is designed to fetch (read) contents from the server and the Post request is designed to submit (save or update) contents on the server. This perspective of receiving and giving provides the base for understanding all their differences.
Difference between get and post request:
- In get request, data are sent as part of the header by appending it to the URL which means that only limited amount of data can be sent because max URL size is 2048 characters. In post request, data are sent as part of body. Size of HTTP packet body can be unlimited (in practice it is controlled by the server configuration) i.e. more data can be sent as part of post request.
- In get request, the data are appended to the URL hence it is displayed in the address bar of the browser. In post request, data are not appended to the URL hence it is not displayed in the address bar.
- In get request, data are transmitted over the network as submitted by the user. In post request, data are transmitted in encrypted form.
- Get request is idempotent, post is not.
- Get can be cached and bookmarked by the browser whereas post can’t be.
- Only ASCII characters are allowed in get request. Both ASCII and binary data are allowed in post request.
Except idempotency, all the differences are self explanatory. An operation is said to be idempotent in mathematics, if multiple applications of it produce the same result as the single application e.g. a=a+0, a=a-0, a=a*1, a=a/1 all are idempotent operations. In the context of HTTP, a request is idempotent if multiple submissions of it from the same user, are treated by the server as single submission and response is returned only once. Initially it seems odd but if we consider the following real life analogy we will understand that it is the right approach.
In real life, you may have asked money from your father or mother for buying different things such as clothes, mobiles etc. Just recall how many times you were given the money at the moment you asked for it, very few! often we need to ask multiple times to get the money we need. If you need Rs. 5000/ and have asked your father 10 times, how much money will he give to you, Rs. 5000 or Rs. 50,000. Of course Rs. 5000 because he knows that all the requests are same. Similarly server treats all the get requests from a user. By making the get request idempotent, server’s performance is improved because duplicate requests are ignored by the server.
Now you will be thinking that if idempotency improves performance than why isn’t post idempotent? To understand the answer, lets take the same analogy in the context of post request. Assume that you have got a job and you want to give your salary each month to your father or mother, should they refuse? Of course not, you may recall now that post is designed to give, If a user wants to save or update contents multiple times why should the server object? It should process each request separately.
Post request may cause side effects if not handled properly. Whenever you make any online transaction, you are cautioned not to press any button or link even the back or next button of the browser and most of the time an empty page containing an animated image is displayed to notify that processing is being done. All this is done to prevent the re-submission of the same transaction because the request type is post.