The GET and POST are the HTTP request methods to indicate the desired action to be performed for a given resource. There are many other request methods are also there like PUT, DELETE, HEAD, CONNECT, OPTIONS, TRACE, PATCH. But the browser software supports only GET and POST method requests. Here we will discuss the differences between the GET and POST method requests i.e. GET vs POST in detail.
URL typed in the browser address bar, and hyperlink generates GET method request, whereas form page can generate either GET or POST method request.
The query string (name/value pairs) is sent inside the URL of a GET request:-
GET /blog/?name1=value1&name2=value2 HTTP/1.1 Host: knowprogram.com
The query string (name/value pairs) is sent in HTTP message body for a POST request:-
POST /blog/ HTTP/1.1 Host: knowprogram.com name1=value1&name2=value2
|It is a default request method if no method is specified for the request.||It is not a default request method.|
|It is designed to get data from the server component.||It is designed to post/send data to the server component.|
|It can carry a limited amount of data ( 2KB to 8KB) along with the request. In most of the browser only 2 KB.||It can carry an unlimited amount of data along with the request.|
|GET supports caching.||POST doesn’t supports caching.|
|In all situations, It support bookmarking of web pages.||In most situations, It doesn’t support bookmarking because there is no query string in the URL.|
|It can send only ASCII characters (text data) along with the request.||It allows both ASCII characters (text), and binary data (non-text data like image, audio, video, and e.t.c.) along with the request.|
|GET method request structure doesn’t contain request body/payload.||POST method request structure doesn’t contain the request body/payload.|
|GET doesn’t provide security because data appears in the browser address bar along with the URL as query String.||POST provides security because the form data doesn’t appear in the browser address bar.|
|Since anyone can see GET method data in the browser address bar therefore it should not be used for sending sensitive information (like password, credit card details, and e.t.c.).||It can be used for sending sensitive information.|
|It supports to add parameter values to history.||It doesn’t support to add parameter values to history.|
|Form data goes to sever as query String appended to the request URL.||Form data goes to the server as request body/payload content.|
|Get mode is idempotent. It is safe to repeat the same request either using the back button or using the refresh button.||POST is not idempotent. It is not safe to repeat the same request because it may post/insert the same data once again.|
|It does not support file upload.||It supports file upload.|
|In general, It performs only read operation on server.||In general, It performs read, writes, and update operations on the server.|
|GET method request is bit faster to go server.||POST method request is a bit slow compared to GET method request.|
|It doesn’t support data encryption.||It supports data encryption.|
We can generate GET method request using:-
a) Typing URL in the browser address bar
b) Using hyperlink to generate the request
c) Using <form> tag without specifying method.
<form action="..."> </form>
d) Using <form> tag with method=”GET”
<form action="..." method="GET"> </form>
To generate POST method request we can use:-
<form action= "..." method= "POST"> </form>
When the request is generated when an internal request structure will be formed having a request line, request header, request body. The request line contains miscellaneous info like protocol version, request URL, and e.t.c. The request header contains info about the browser. And the Request body contains form data.
GET vs POST Performance
GET method request is bit faster to go server. POST method request is bit slow compared to GET method request.
GET method performance is better because of its simple nature of appending the values in the URL. Whereas the POST method performance is low compared to the GET method because of time spent in including form data in the request body.
GET vs POST Caching
GET supports caching whearas POST doesn’t support caching.
The cache is a temporary memory that holds data for a temporary period. In client-server communication, the cache at the client-side holds server-supplied data and uses that data across multiple same requests and this reduces the network round trips between client and server.
GET method request doesn’t have a request body because it will send form data to the server as a query string appended to the URL. The Post mode request contains form data as a request body.
The cache holds results against the URL with the query string. Since the GET method request contains a query string in the URL so it supports caching. Since the POST method request doesn’t contain a query string in the URL so it doesn’t support caching.
If the data is changing quickly then it is recommended to disable caching even for the GET method like displaying stock share values, displaying the score of live games e.t.c
GET vs POST Which is Secure
The POST method is more secured compared to the GET method. The GET method doesn’t support data encryption but the POST method supports data encryption.
For GET method, the form data appears in the browser address bar along with the URL as the query string. Example:- https://www.knowprogram.com/?s=computer+basic; Here “computer basic” was the data which appeared in the browser address bar and anyone can see them. Therefore GET method doesn’t provide security.
For POST method request, the form data goes to the server as request body/payload content and it doesn’t appear in the browser address bar. Therefore POST method provides security.
When to Use GET vs POST
We should use GET method request if we want to read data without changing the state and use the POST method request if we want to update the state on the server.
Since in GET method values are visible in the URL and anyone can see the data appeared in the browser address bar, therefore, GET method should not be used for sending sensitive information (like password, credit card details, and e.t.c.). Whereas the POST method can be used to send sensitive information.
GET vs POST Best Practices
Some of the best practices for GET and POST method requests are:-
- Use GET for safe actions and POST for unsafe actions.
- Use POST when dealing with sensitive data.
- And use POST when dealing with long requests.
- Use GET in AJAX environments.
GET and POST in Java
In servlet, use service(-,-) or doGet(-,-) method to process the GET mode request, whereas to process POST mode request use doPost(-,-) method.
To give the instruction to disable the caching we use the following code in servlet:-
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!