Difference between PUT and POST Methods
PUT and POST are HTTP verbs used to create or update new resource. Every times it creates confusion that when to use PUT and when to use POST.A most popular misconception for PUT and POST that a POST is used to create a resource, and a PUT is used to update/change one.
In SQL terms, POST is an UPDATE and PUT is an INSERT.Both PUT and POST can be used to create new resources. PUT is update or create with a user-supplied URL while POST is created with a server generated URL. There are no custom actions at all. That is one of the key principles of REST.PUT puts a page at a specific URL. If there’s already a page there, it’s replaced and If there’s no page there, a new one is created.
PUT is used when you allow the users to specify the resource identifier of the newly created resource. But as PUT is idempotent, you must send all possible values.i.e. if you aren’t sure if a request succeeded, try it again.
POST is used to create resources when we don’t know the resource identifier. Once POST executes,the status of “201 Created” is returned and the location of the newly created resource. This allows the client to access the new resource later.
Some considerations for PUT and POST:
- Do you name your URL objects you create explicitly or leave it on server ? If you name them then use PUT. If you let the server decide then use POST.
- PUT is idempotent so if you PUT an object twice it has no effect. So we would use PUT when possible.
- We can update or create a resource with PUT with the same object URL.
- Using POST we can have 2 requests coming in at the same time making modifications to a URL, and they may update different parts of the object.
Difference between PUT and POST :
|PUT is update or create with a user-supplied URL.||POST is create with a server generated URL.|
|PUT should be used when the client specifies the location of the page.||POST is used when the client sends the page to the the server, and the server then tells the client where it put it.|
|PUT is a much more limited operation.||POST has not limited operation.|
|The URI in a PUT request identifies the entity enclosed with the request.||The URI in a POST request identifies the resource that handles the enclosed entity.|
|PUT is supposed to accept the body of the request, and then store that at the resource identified by the URI.||POST is supposed to initiate an action on the server. It could be a different URI, or it could be a different action.|
|obj.attribute = value # A PUT request.||obj.set_attribute(value) # A POST request.|
Sample PUT Request
For a new resource:
PUT /question/<new_question> HTTP/1.1 Host URL: www.example.com
To overwrite an existing resource:
PUT /question/<existing_question> HTTP/1.1 Host URL: www.example.com
Sample POST Request
POST /question HTTP/1.1
Host URL: www.example.com