Open In App

Difference between HTTP GET and POST Methods

Improve
Improve
Like Article
Like
Save
Share
Report

HTTP (Hypertext Transfer Protocol) specifies a collection of request methods to specify what action is to be performed on a particular resource. The most commonly used HTTP request methods are GET, POST, PUT, PATCH, and DELETE. This article covers the 2 most common HTTP request methods, i.e. the GET & POST Methods among the rest of the methods.

HTTP GET

The Hypertext Transfer Protocol(HTTP) Get method is mainly used on the client (Browser) side to send a request to a specified server to get certain data or resources. Using this method the server should only let us receive the data and not change its state. Hence it is only used to view something and not to change it. The Get method is one of the most used HTTP methods. The request parameter of the get method is appended to the URL. Get request is better for the data which does not need to be secure (It means the data which does not contain images or Word documents).

Example: In the following HTML code we have created a form with text fields such as Username and City. we have also included a PHP file getmethod.php where our data would be sent after we click the submit button.

index.html




<!DOCTYPE html>
<html>
 
<body>
    <form action="getmethod.php" method="GET">
        Username:
          <input type="text" name="username" /> <br>
        City:
          <input type="text" name="city" /> <br>
        <input type="submit" />
    </form>
</body>
 
</html>


In the following PHP code using the GET method we have displayed the Username and city.  

getmethod.php




<!DOCTYPE html>
<html>
 
<body>
    Welcome
    <?php echo $_GET["username"]; ?> </br>
    Your City is:
    <?php echo $_GET["city"]; ?>
</body>
 
</html>


Output: Data passed in GET method is clearly visible in the address bar, which can compromise the security.

HTTP POST

The Hypertext Transfer Protocol(HTTP) Post method is mainly used at the client (Browser) side to send data to a Specified server in order to create or rewrite a particular resource/data. This data sent to the server is stored in the request body of the HTTP request. Post method eventually leads to the creation of a new resource or updating an existing one. Due to this dynamic use, it is one of the most used HTTP methods. It is not one of the most secure methods because the data that is been sent is included in the body of the request and not in the URL. Post request is better for the data which needs to be secure (It means the data which contains images or word documents).

Example: In the following HTML code we have created a form with text field as Username and Area of study. we have also included a PHP file postmethod.php, where our data would be sent after we click the submit button.

index.html




<!DOCTYPE html>
<html>
 
<body>
    <form action="postmethod.php" method="post">
        Username:
          <input type="text" name="username" /> <br>
        Area of Study:
          <input type="text" name="area" /> <br>
 
        <input type="submit" />
    </form>
</body>
 
</html>


In the following PHP code using the POST method we have displayed the Username and Area of study.

postmethod.php




<!DOCTYPE html>
<html>
 
<body>
    Welcome
    <?php echo $_POST["username"]; ?> </br>
    YOur Area of Study is:
    <?php echo $_POST["area"]; ?>
</body>
 
</html>


Output: Data passed in POST method is not shown in the address bar, which maintains the security.

Difference between HTTP GET and HTTP POST

HTTP GET

HTTP POST

In GET method we can not send large amount of data rather limited data of some number of characters is sent because the request parameter is appended into the URL.  In POST method large amount of data can be sent because the request parameter is appended into the body.
GET request is comparatively better than Post so it is used more than the Post request. POST request is comparatively less better than Get method, so it is used less than the Get request.
GET requests are only used to request data (not modify) POST requests can be used to create and modify data.
GET request is comparatively less secure because the data is exposed in the URL bar. POST request is comparatively more secure because the data is not exposed in the URL bar.
Request made through GET method are stored in Browser history. Request made through POST method is not stored in Browser history.
GET method request can be saved as bookmark in browser. POST method request can not be saved as bookmark in browser.
Request made through GET method are stored in cache memory of Browser.  Request made through POST method are not stored in cache memory of Browser.
Data passed through GET method can be easily stolen by attackers as the data is visible to everyone.GET requests should never be used when dealing with sensitive data Data passed through POST method can not be easily stolen by attackers as the URL Data is not displayed in the URL
In GET method only ASCII characters are allowed. In POST method all types of data is allowed.
In GET method, the Encoding type is application/x-www-form-urlencoded  In POSTmethod, the encoding type is application/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data


Last Updated : 11 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads