Register Login

Difference between PUT and POST Method

Updated Jan 07, 2020

Every HTTP method comprises of a given set of features and functionality and is to be used for specific purposes only. While most HTTP methods like GET, HEAD, DELETE, CONNECT, OPTIONS, TRACE, etc. are easily understood by users, the methods like PUT and POST tend to be a tad confusing in context to their usage. In what follows below, we will be discussing the definition of PUT, the definition of POST, the difference between PUT and POST, the right way of using POST and PUT, etc. We begin with a comparative chart depicting the difference between the POST and PUT. Read on for more.

PUT vs. POST

Basis of Differentiation PUT POST
Requests for RFC-2616 states that the PUT method places a request for an enclosed entity to be stored in the supplied Request-URI. The POST method requests the origin server to accept the entity that is enclosed in the request. The acceptance has to be in the form of a new subordinate of the identified resource, as stated by the Request-URI under the Request-Line.
Action In case the Request-URI relates to an existing resource, then an update type of operation will take place. Else, a create operation occurs in case the Request-URI happens to be a valid resource URI. This is under the assumption that the client is permitted to assess the resource identifier. The action of this command mainly pertains to the POST request-URI being a collection URI.
Syntax PUT /questions/{question-id} POST /questions
Nature The PUT method is idempotent. In case the user sends multiple retries of the same request, then each request is equal to a specific/single request modification. The POST method is NOT idempotent. In case a request is retried multiple times, then various resources with an equal number of URIs get created on the server.
Usage PUT is utilized when modifications to a unique resource, which already exist in the resources collection, is requested. PUT will replace the resource entirely. The PATCH command is to be used in case the request relates to updating a specific portion of the resource. POST is used when the request pertains to adding a child resource in the existing resources collection.
Caching of response As PUT is idempotent, the answer can be cached. Responses to PUT cannot be cached. The exception being when the answer expires the header fields or incorporates appropriate cache-control elements.
General application PUT is typically used for UPDATE operations POST is generally used for CREATE operations

PUT Method

The PUT method replaces all the elements that currently exist in the targeted URL, entirely, with the desired components. An altogether new resource can be created from scratch, or by overwriting on the existing resources, provided the exact Request-URI is known.

Example to send PUT request:

// Example to send PUT request
$data = array("datafiled" => "datavalue");
// please give full url like yoursite.com/filename
$ch = curl_init('https://www.yoursite.com/test/result.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

$response = curl_exec($ch);
if(!$response) {
	return false;
}else{
	echo $response;
}

POST Method

The POST method is useful for sending user-generated data that has to reach a targeted web server. For instance, a POST method can be put to use when a comment has to be posted on a forum, or if the user wishes to upload his or her profile picture. The POST method in HTTP is capable of being used even if the URL for the newly created resource is unknown to the user. It is useful when a new forum thread has to be created, and the path of the thread in the reckoning is unspecified. In other words, the POST method comes in handy for creating a subordinate or child of the existing resource, as understood by the Request-URI.

Example to send POST request:

// Example to send POST request
$data = array("datafiled" => "datavalue");
// please give full url like yoursite.com/filename
$ch = curl_init('https://www.yoursite.com/test/result.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

$response = curl_exec($ch);
if(!$response) {
	return false;
}else{
	echo $response;
}

The output of PUT and POST Method

if($_SERVER['REQUEST_METHOD'] == 'GET') {
    echo "This is a GET request Data: ".$_GET['datafiled'];
} elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
    parse_str(file_get_contents("php://input"),$post_vars);
	echo "This is a PUT request Data: ".$post_vars['datafiled'];
}elseif($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo "This is a POST request Data: ".$_POST['datafiled'];
}

A Key difference between POST and PUT Method

  • The following vital differences between PUT vs. POST will help gain clarity with regards to which method to use in specific circumstances.
  1. PUT method serves to be idempotent. Regardless of the number of times a given request is sent, it will not fail to provide the same result. Conversely, the POST method not being idempotent will offer different results in case the same POST request is sent multiple times. In other words, a child or new subordinate of the existing class will come into being every time the method is called. Therefore, the choice between the use of PUT vs. POST has to be based on this feature of the idempotence of POST and PUT.
  2. Another fundamental difference in HTTP methods POST and PUT can be seen as the Request-URI. The URI specific to a POST request ascertains the resource that is capable of handling the enclosed entity. Conversely, the URI in the PUT request ascertains the entity that is enclosed within the request.

Conclusion

POST and PUT, two significant HTTP methods, are often used interchangeably. The essential difference of the idempotence of action is the best way of determining whether to use POST or PUT. In case you have any further queries with regards to the difference between PUT and POST, then do write to us, we will be glad to assist you at the earliest.


×