Open In App

Difference between HTTP GET and POST Methods

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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 HTTP GET method requests data from a server without altering its state. It appends parameters to the URL, making it suitable for retrieving non-sensitive data. Commonly used for viewing content, GET is ideal for requests that don’t involve data modification.

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.

<!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.  

<!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 HTTP POST method sends data from the client to the server to create or update resources, storing data in the request body. It’s suitable for secure data transfer, like images or documents, with security relying on encryption (HTTPS), authentication, and validation.

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.

<!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.

<!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 dataData 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

HTTP GET and POST Methods – FAQs

How are data sent in HTTP GET and POST requests?

GET sends data appended to the URL as query parameters, while POST sends data in the request body.

Is HTTP GET or POST more secure?

POST is generally more secure as it sends data in the body, but true security depends on using HTTPS for both methods.

Which method should I use for submitting forms: GET or POST?

Use GET for simple, non-sensitive data retrieval, and POST for forms involving sensitive data or changes to server-side resources.

Can HTTP GET requests change server state?

No, GET requests are intended for data retrieval without modifying the server state.

Why is POST preferred over GET for sending large amounts of data?

POST can handle large data in the request body, whereas GET is limited by URL length restrictions.



Similar Reads

three90RightbarBannerImg