Should we use the http header PUT or PATCH when updating a record in a CRUD system?

CRUD RESTful APIs

CRUD or Create, Read, Update, Delete are the basic operations required to create a RESTful interface for a database backed website. From the API point of view, the following http header methods are used for the listed operations:

API actionhttp header method
CreatePUT
ReadGET
UpdatePUT/PATCH
DeleteDELETE

POST or GET for search?

I would always choose to use GET for a data retrieval operation from the server, there are three reasons for this:

  1. The URL is simple, and can be shared to another person for them to GET the same view of the data as me
  2. Search engines can index requests using a GET method, so the data can be indexed
  3. GET is an idempotent operation (an operation that produces the same results no matter how many times it is performed), whereas POST is typically used to modify data on the database

PUT vs PATCH for update?

Well, this is a little more subtle. Basically we need to consider the data set, and how much of the data set is being changed. Let’s consider that I have a record:

{
    "name": "Luke",
    "home": "Tattoine",
    "alliance": "Rebel scum",
    "father": "unknown"
}

My basic rule would be as follows:

  • If I am going to change all of the fields in the record, then use PUT
  • If I am going to modify only some fields in the record, then use PATCH

So, if I want to add the following to the request body:

{
    "father": "Darth Vader"
}

then I would use a PATCH request and only send the field that needed to be updated. Easy!