Online Tutorials & Training Materials | STechies.com
Register Login

Difference between PUT and POST with Comparison Chart

|| || 0

Difference between PUT and POST with Comparison Chart
Stechies

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 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 essentially 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/ singular request modification.

The POST method is NOT idempotent. In case a request is retried multiple times, then multiple resources with equal number of URIs gets created on the server.

Usage

PUT is utilized when modifications to a singular resource, which already exists in the resources collection, is requested. PUT will replace the resource completely. 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 response can be cached.

Responses to PUT cannot be cached. The exception being when the response expires the header fields or incorporates appropriate cache control elements.

General application

PUT is typically used for UPDATE operations

POST is typically used for CREATE operations

What is PUT Method

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

Example:

// 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;
}

What is 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/ or 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:

// 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;
}

Combined Result of above example 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'];
}

Key difference between POST and PUT

The following key 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 on the basis of this feature of idempotence of POST and PUT.

2. Another fundamental difference in HTTP methods POST and PUT can be seen in the nature of 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 important difference pertaining to the idempotence of action is the best way of determining whether to use POST or PUT. In case you have any further query with regards to the difference between PUT and POST then do write to us, we will be glad to assist you at the earliest.


Related Articles