36

Lets say I have rest endpoint for my Driver resource. I have PUT method like this

myapi/drivers/{id}

{body of put method}

I need to add functionality which will allow to 'enable' and 'disable' driver

Is it good idea to create new endpoint for that like this?

PUT myapi/drivers/{id}/enable/false

or it is better to use existing endpoint ? One problem with using existing endpoint is that driver has lot's of fields(almost 30) and sending all those fields just for updating only 'enabled' or 'disable' driver is something overkill.

What do you think?

user1321466
  • 1,559
  • 2
  • 15
  • 27

2 Answers2

37

This is exactly what the HTTP method PATCH is made for. It is used in cases where the resource has many fields but you only want to update a few.

Just like with PUT, you send a request to myapi/drivers/{id}. However, unlike with PUT, you only send the fields you want to change in the request body.

Creating endpoints like myapi/drivers/{id}/enable is not very RESTful, as "enable" can't really be called a resource on its own.

For an example implementation of a Spring PATCH endpoint, please see this link.

Leroy
  • 2,822
  • 15
  • 25
  • agreed! lots of people think `GET` and `POST` only... plus one – Eugene Dec 21 '17 at 10:04
  • 1
    @Synch what do you think about `myapi/drivers/{id}/is_enabled` and PATCH is_enabled? I would like to communicate to my API users that PATCH is only designed to "toggle" some status in the context of drivers resource. If they want to update any other fields just PUT to `myapi/drivers/{id}`. Does it make sense? – Kamil Latosinski Sep 09 '18 at 02:44
  • 1
    There are two things you can do. You can create a PATCH endpoint and limit it to only `status`. If the client tries to update anything else, you throw a `406`. In the documentation, you'd need to explain the endpoint is only suitable for one attribute. The other approach is to create a "sub-resource" `myapi/drivers/{id}/status`. See [link](https://stackoverflow.com/a/37544666/5771446) for a much more detailed answer. – Leroy Sep 10 '18 at 12:25
2

Use PATCH Http metod to update one field

PATCH myapi/drivers/{id}/enable
whaat
  • 47
  • 4