1

I am trying to figure out how can I implement a HTTP patch in my application. I have never used it before. I use Express.js and sequelize (MySQL).

I looked on SO and on internet for real life example, but could not find anything of real value. Those links gave me better understanding what is PATCH, but I still fail to see its benefit in my case. It must be something I am missing.

This is my simplified setup:

router:
basePath: 
    controller: PutUser
    method: PUT
    path: /user/:userId/

Payload:
{email: 'newemail@hello.com'}

Controller:
Then here I find the user by its id and give the payload. User gets updated

What is confusing me, is if I were to implement a PATCH, the only things that changes is in router, I use PATCH instead. I also use userId to find the user and change its email. What am I missing? Is this how it is supposed to be?

Community
  • 1
  • 1
Amiga500
  • 5,874
  • 10
  • 64
  • 117

1 Answers1

1

You are not missing anything. The difference between PUT and PATCH is more semantically. The method itself does not deliver much additional information/requirement in HTTP level. According to RFC5789, using PUT or PATCH depends on your server-side logic:

The difference between the PUT and PATCH requests is reflected in the way the server processes the enclosed entity to modify the resource identified by the Request-URI.

Thus, if your user resource has many fields, such as email, name, address, age etc, then PATCH is the better choice if you only want to change email. If your user resource only have email field, or if you want to reset all fields information, then PUT is the better choice.

I guess you are confused because this "update email" operation is an "override" operation, which makes it very similar to PUT or POST. There is a more appropriate example though (pseudocode):

PATCH /user/<userId>
Payload:
{
    patchField: 'age', 
    patchOperation: 'X+1'
}
Community
  • 1
  • 1
shaochuancs
  • 15,342
  • 3
  • 54
  • 62