Winters Huang
Winters Huang

Reputation: 791

How do I remove a sub resource by restful PATCH

When we use PATCH, we are partially updating a resource. What's the best practice if I want to remove part of the resource?

For example: we have a Person resource:

Person:
{
  id: 3,
  name: 'test',
  companyId: 1,
}

Lets say this person leaves the company and has no job, then the companyId should be set as null in the DB. When I want to remove the companyId in Person via API, shall I set the companyId in the payload as null as well?

PATCH: /person/3

Person:
{
  id: 3,
  companyId: null,
}

I used to remove an object by setting it as {}, or remove an array by setting it as [], but I am not 100% sure what's the best practice for a primitive property (like the companyId in this case). Any idea?

Thanks

Upvotes: 0

Views: 401

Answers (1)

Alex Marculescu
Alex Marculescu

Reputation: 5770

Looking at the RFC 6902 (which defines the Patch standard), from the client's perspective the API could be called like

PATCH /person/3

[
    { "op": "remove", "path": "/companyId"}
]

I think the exact internal implementation (nullable int vs 0/-1 as default) is up to you.

Upvotes: 2

Related Questions