Rest Api: When to use Post, PUT, PATCH and Delete

rest api post example
put vs post vs patch
how to send post request to rest api
put vs patch
rest api delete example
rest api put example java
rest put example
http delete request body

I am working on a restful api and I need to update a resource (i.e. a customer detail record with 10 fields).

On Add request, I send a Post request with complete record. On update request, I send a PUT request with complete record of 10 fields. On Verify request, I send a PUT request with just two fields i.e. recordId and versionNo. On Delete request, I send a DELETE request with two fields in HttpOptions.

I have few questions that:

  • Although, it a restful api but specific application which would be used by an angular application, So should I return data in response of POST/PUT requests.

  • Should I use PATCH in case of Verify (or anyother action where just recordId and versionNo send to server to change some fields) or it is OK to use PUT.

  • To make uniformity, should I send data in body of delete request as I need recordId and versionNo to delete a record.

Should I use PATCH in case of Verify (or anyother action where just recordId and versionNo send to server to change some fields) or it is OK to use PUT.

In RESTful API designs, PUT requests are generally used to add or replace an entire resource, whereas a PATCH should be just used to update an existing resource. A PUT request is called "idempotent" - no matter how many times you send a PUT response, you should get the same result. A PATCH is not idempotent.

example:

PATCH /Cars/vauxhall-astra/engine --> This request would be used to only update the engine of my already existing vauxhall astra

PUT /Cars/renault-clio --> This request would create a new Renault Clio or, if it already exists, replace the entire Clio using the data specified in my request. A Clio would then be guaranteed to exist after my request is successful, regardless of whether it existed or not before.

Although, it a restful api but specific application which would be used by an angular application, So should I return data in response of POST/PUT requests.

Totally up to you, returning data from a POST/PUT is fine - especially if it saves you having to make extra GET api requests. Just always make sure you are only ever returning the data you need from a response.

To make uniformity, should I send data in body of delete request as I need recordId and versionNo to delete a record.

Again it's totally up to you. Whether you use query parameters (e.g. DELETE cars?id=123) or a request body is just your preference, there's nothing in REST that has rules for this.

REST – PUT vs POST, Use PATCH if request updates part of the resource. Use POST when you want to add a child resource under resources collection. Though PUT is� GET, POST, PUT, MERGE and PATCH these methods are responsible for CREATE, READ, UPDATE and DELETE (REST CRUD) operation in SharePoint. In simple understanding we can define as below: GET : GET method is used to retrieve or get the information from the given server using a given URI i.e Uniform Resource Identifier.

REST Response

A RESTful API MUST always answer with HTTP codes to client requests:

Success and error responses are a vital part to define how an API is used correctly.

Refer to this guide to solve all your RESTful related questions.


PATCH/PUT

From Wikipedia:

The main difference between the PUT and PATCH method is that the PUT method uses the request URI to supply a modified version of the requested resource which replaces the original version of the resource whereas the PATCH method supplies a set of instructions to modify the resource. If the PATCH document is larger than the size of the new version of the resource sent by the PUT method then the PUT method is preferred.

Also:

Using the PUT method consumes more bandwidth as compared to the PATCH method when only a few changes need to be applied to a resource. But when the PATCH method is used, it usually involves fetching the resource from the server, comparing the original and new files, creating and sending a diff file. On the server side, the server has to read the diff file and make the modifications. This involves a lot of overhead compared to the PUT method.[11] On the other hand, the PUT method requires a GET to be performed before the PUT and it is difficult to ensure that the resource is not modified between the GET and PUT requests.

So I will use PATCH for verifying a resource.


DELETE

Normaly, for DELETE requests, the client specifies the id of the resource and pass it ass a Path Variable on the URL:

curl -X DELETE http://example.com/resource/{id}

But you can pass a body on the request also. This possibility is stated by MDN Mozilla Web DOCS:

Request has body - May

Successful response has body - May

Rest Api: When to use Post, PUT, PATCH and Delete, Should I use PATCH in case of Verify (or anyother action where just recordId and versionNo send to server to change some fields) or it is OK to� The project create a set of Rest-APIs for POST/GET/UPDATE/DELETE APIs: /api/customers – GET all customers /api/customers/:id – GET a customer by Id '/api/customers – POST a customer

Even though others have answered the question in details before me but still I'm posting this just to provide a handy short difference between all of these HTTP methods

1.HTTP Post:It is used to create an item

2.HTTP Put:It is used to update an item

3.HTTP Patch:It is used to partially update an item

4.HTTP Delete:It is used to delete an item

HTTP Methods for RESTful Services, HTTP methods tutorial on how to use them for RESTful API or Web Service. methods, as they are properly called) are POST, GET, PUT, PATCH, and DELETE. (This definition is specifically designed to argue about PUT and POST, and e.g. doesn't make much sense on GET and POST, as it doesn't care about persistence). Now, by fixing x: Res (informatically speaking, using currying), PUT(x: Res) and PATCH(x: Res) are univariate functions of type Res → Res.

What are GET, POST, PUT, PATCH, DELETE? A walkthrough with , GET, POST, PUT, PATCH, and DELETE are the five most common HTTP We'll also get our hands dirty by using JavaScript's Fetch API to� HTTP POST. Use POST APIs to create new subordinate resources, e.g., a file is subordinate to a directory containing it or a row is subordinate to a database table. When talking strictly in terms of REST, POST methods are used to create a new resource into the collection of resources.

When to use PUT or POST, When should we use PUT and when should we use POST? The next example is perfectly valid if your API provides this functionality: PUT DELETE < resoutce_uri> and PATCH <resource_uri> stands for the meaning of the respective verb. Alternatively, use POST to create new resources and provide the client-defined ID in the body representation—presumably to a URI that doesn't include the ID of the resource (see POST below). On successful update, return 200 (or 204 if not returning any content in the body) from a PUT.

7 HTTP methods every web developer should know and how to test , Simlar to POST, PUT requests are used to send data to the API to update A PATCH request is one of the lesser-known HTTP methods, but I'm This method is one of the more common in RESTful APIs so it's good to know how it works. Be careful when testing DELETE methods, make sure you're using� create - POST ; read - GET; update - PUT; delete - DELETE; PATCH: Submits a partial modification to a resource. If you only need to update one field for the resource, you may want to use the PATCH method. Note: Since POST, PUT, DELETE modifies the content, the tests with Fiddler for the below url just mimicks the updations. It doesn't delete or

Comments
  • PATCH is not idempotent. Does it mean, when a PATCH request is made, api update it iff the resource exists. If resource doesn't exist, it will not update the resource.
  • Yes that's correct. Remember that idempotency means the same request should always produce the same result. A PATCH cannot be idempotent, because imagine you send a PATCH request that changes the location of a resource. If you sent through the exact same PATCH request through again it would fail because the location has since changed.
  • In my case, a PATCH request only be made when a record exists and a verify action is available. So in this scenario, it would be safe to use PATCH, instead of PUT, while I just need to update statusCode of that particular record.
  • Yeah, you should use PATCH in this situation
  • My question was not about these definitions. My question was very specific about the points mentioned in question.