What is PUT Method?
PUT method is used to update resource available on the server. Typically, it replaces whatever exists at the target URL with something else. You can use it to make a new resource or overwrite an existing one. PUT requests that the enclosed entity must be stored under the supplied requested URI (Uniform Resource Identifier).
What is POST Method?
POST is a method that is supported by HTTP and depicts that a web server accepts the data included in the body of the message, which is requested. POST is often used by World Wide Web to send user generated data to the web server or when you upload file.
Differences between PUT and POST in REST APIs
Here is the important difference between PUT and POST method:
Example of PUT
Here is the webserver example of a PUT method: HTTP PUT http://www.google.com/users/234 HTTP PUT http://www.google.com/users/234/accounts/567 Request
PUT /new.html HTTP/1.1 Host: example.com Content-type: text/html Content-length: 20
New File
Responses If the target resource having current representation and is modified with the state of the enclosed representation, then the server should send two responses. The first response code is 200 (OK), and the second response code is 204 (No Content). If the target resource does not have any representation, then the server should inform the user by sending a 201 code (Created) response.
HTTP/1.1 201 Created Content-Location: /new.html
Example of POST
Here is an example of POST method: HTTP POST http://www.google.com/users HTTP POST http://www.google.com/users/234/accounts A form using the default application/x-www-form-urlencoded content type:
POST /test HTTP/1.1 Host: abc.example Content-Type: application/x-www-form-urlencoded Content-Length: 40
field1=value1&field2=value2
Testing an API with PUT requests
Here are the steps to test API with PUT requests:
Step 1) Update resources with PUT request. Step 2) Use GET method for resource. If the PUT request success, you will receive new data. This method will fail if the supplied data in the request is invalid. Therefore, it will not update anything.
Testing an API with POST requests
Here are the steps to test API with POST requests:
Step 1) Create a resource using POST request and make sure that it returns 200 status code. Step 2) Make a GET request for that resource and save the data in the correct format. Step 3) You have to add tests which ensure POST requests fail with incorrect data.
Advantages of PUT Method
Here are pros/benefits of using PUT method:
It helps you to store the supplied entity under the supplied URI If the supplied entity already exists, then you can perform the update operation, or you can create with that URI. You can create a resource as many times as you like. Creating a resource with PUT method is very easy. You do not need to check whether the user has clicked the submit button multiple times or not. It can identify the entity enclosed with the request.
Advantages of POST Method
Here are pros/benefits of using POST method:
This method helps you to determine resource URI. Specifying a new resource location header is very easy using location header. You can send a request to accept the entity as a new subordinate of the resource, which is identified by the URI. You can send user-generated data to the web server. It is very useful when you do not know URL to keep any resource. Use POST when you need the server, which controls URL generation of your resources. POST is a secure method as its requests do not remain in browser history. You can effortlessly transmit a large amount of data using post. You can keep the data private. This method can be used to send binary as well as ASCII data.