The difference between the PUT, POST, GET, DELETE, and PATCH IN HTTP commands:
The most commonly used HTTP verbs POST, GET, PUT, DELETE are similar to CRUD operations (create, read, update and delete) in a database. We indicate these HTTP verbs in case of capitalization . So below is a comparison between the two.
- create - POST
- read - GET
- update - PUT
- delete - REMOVE
PATCH: Represents a partial modification of a resource. If you need to update only one field for a resource, you can use the PATCH method.
Note:
Since POST, PUT, DELETE change the content, the tests with Fiddler for the URL below simply mimic the updates. It does not delete or change in fact. We can just see the status codes to check if inserts, updates, deletes are occurring.
URL: http://jsonplaceholder.typicode.com/posts/
1) GET:
GET is the simplest type of HTTP request method; the one that browsers use every time you click on a link or enter a URL in the address bar. It tells the server to pass the data identified by the URL to the client. Data should never change on the server side as a result of a GET request. In this sense, the GET request is read-only.
Verification with Fiddler or PostMan: We can use Fiddler to verify the response. Open Fiddler and select the Compose tab. Enter the verb and URL as shown below and click "Run" to check the response.
Verb: GET
URL: http://jsonplaceholder.typicode.com/posts/
Answer: You will get an answer like:
"userId": 1, "id": 1, "title": "sunt aut ...", "body": "quia et suscipit ..."
In a “happy” (or non-error) path, GET returns a view in XML or JSON and an HTTP 200 response code (OK). In case of an error, it most often returns 404 (NOT FOUND) or 400 (BAD REQUEST).
2) POST:
The POST verb is mainly used to create new resources. In particular, it is used to create subordinate resources. That is, obey any other (e.g. parent) resource.
If successful, return HTTP status 201 by returning the Location header with a link to the newly created resource with HTTP status 201.
Verification with Fiddler or PostMan: We can use Fiddler to verify the response. Open Fiddler and select the Compose tab. Enter the verb and URL as shown below and click "Run" to check the response.
Verb: POST
URL: http://jsonplaceholder.typicode.com/posts/
Request body:
data: {title: 'foo', body: "bar", user ID: 1000, Id: 1000}
Answer: You will receive a 201 response code.
If we want to check the inserted record with Id = 1000, change the verb to Get and use the same URL and click Run.
As mentioned earlier, the above URL allows read only (GET), we cannot read the updated data in real time.
3) PUT:
PUT is most often used to update capabilities, PUT is for a known resource URI with a request body containing a recently updated representation of the original resource.
Verification with Fiddler or PostMan: We can use Fiddler to verify the response. Open Fiddler and select the Compose tab. Enter the verb and URL as shown below and click "Run" to check the response.
Verb: PUT
URL: http://jsonplaceholder.typicode.com/posts/1
Request body:
data: {title: 'foo', body: "bar", user ID: 1, Id: 1}
Answer: On a successful update, it returns 200 (or 204 if it does not return any content in the body) from PUT.
4) REMOVE:
DELETE is pretty easy to understand. It is used to delete a resource identified by a URI.
If the deletion is successful, return the HTTP status 200 (OK) along with the response body, possibly representing the deleted element (often requiring too much bandwidth) or a wrapped response (see "Return Values" below). Either so, or return the HTTP status 204 (NO CONTENT) without the response body. In other words, a status of 204 without a body or a JSEND-style response and an HTTP status of 200 are recommended answers.
Verification with Fiddler or PostMan: We can use Fiddler to verify the response. Open Fiddler and select the Compose tab. Enter the verb and URL as shown below and click "Run" to check the response.
Verb: DELETE
URL: http://jsonplaceholder.typicode.com/posts/1
Answer: Upon successful removal, the HTTP status 200 (OK) is returned along with the response body.
Example between PUT and PATCH
Put
If I had to change my name, send a PUT request for update:
{"first": "Nazmul", "last": "hasan"} So, here, to update the name, we need to send all the data parameters again.
PATCH:
A request for correction indicates that we will only send the data that we need to change, without changing or affecting other parts of the data. Example: if we need to update only the name, we pass only the name.
Please refer to the links below for more information:
https://jsonplaceholder.typicode.com/
https://github.com/typicode/jsonplaceholder#how-to
What is the main difference between PATCH and PUT requests?
http://www.restapitutorial.com/lessons/httpmethods.html