SSR
SSR

Reputation: 115

HTTP PATCH ADD/Remove field on resource

HTTP PATCH is used for partial updates for the resource. And as per my understanding following updates are possible on resource which are relevant to PATCH (Could be more but I need to know exactly how these two are handled differently)

  1. Updating a field like Mobile Number on Customer resource
  2. Add a New Telephone Number field on existing Customer Resource like a Telephone Number[4] is introduced as a new field

I just want to know that whether PATCH should handle the second scenario or not? If yes then how should it be and how it be differ from the first one?

Upvotes: 3

Views: 8545

Answers (2)

Thierry Templier
Thierry Templier

Reputation: 202176

Yes, you can use but a method PATCH but I think that in your case (second scenario) using the method POST is more convenient:

POST /users/<userid>/phones
Content-type: text/plain
+33 3 12 34 56 78

201 Created

Edited:

Following your comment, the method PATCH is what you need. Here are some links that could help you to define the format of the payload for your request:

Otherwise you can notice that OData directly provides the data in the payload. See this link http://www.odata.org/getting-started/basic-tutorial/, section "Update an Entity".

Hope it helps you, Thierry

Upvotes: 0

Pedro Werneck
Pedro Werneck

Reputation: 41898

PATCH requires a document format which is able to describe a set of changes to modify the target resource. The format itself is not standardized by HTTP and its up to you to choose the format you're going to use. This might be a simple diff file, or something designed specifically for serialized formats, like the JSON-patch format, for instance.

Considering that, PATCH can be used for any partial updates that can be performed by the patching algorithm you decided to use. If the media-type for the Customer resource allows a telephone number to be added as a new field, it's perfectly fine to use PATCH to add that, if the patching algorithm used can perform additions.

Upvotes: 3

Related Questions