REST API Design - add / remove an item in an array field of a resource -


let's assume have following rest resource collection endpoint:

/products 

the resource looks this:

get /products/123 accept: application/json  {     "id": "123",     "name": "shampoo",     ...     "ingredients": [         "sodium lauryl sulfate",         "sodium laureth sulfate",         "hydrochloric acid",         ...     ] } 

now, let's want update resource, , add or remove ingredient / ingredients field. 1 way get resource, manipulate array, , put or patch back. however, pretty chatty protocol, , poses scaleability problem, if array big.

what best way achieve add / remove operation without having source array?

i explored following: json patch specification seems spec not rest. uses same resource different "operation" representation, entirely different resource's representation.

i keep as possible same representation of resource put / patch method appearing in get.

here thoughts on matter.

  1. change string type item object
  2. add meta field object called $op

so in design, here how resource looks on get

get /products/123 accept: application/json  {     "id": "123",     "name": "shampoo",     ...     "ingredients": [         {             "name": "sodium lauryl sulfate"         },         {             "name": "sodium laureth sulfate"         },         {             "name": "hydrochloric acid"         },         ...     ] } 

and if want remove item ingredients array:

patch /products/123 content-type: application/json accept: application/json  {     "ingredients": [         {             "$op": "remove",             "name": "hydrochloric acid"         }     ] } 

or add back:

patch /products/123 content-type: application/json accept: application/json  {     "ingredients": [         {             "$op": "add",             "name": "hydrochloric acid"         }     ] } 

does has better way? there standard on subject?

in opinion best solution provide rest endpoint product's ingredients management. when in addition provide links in hateoas manner, able controll array right after getting product resource.

consider representation:

get /products/123 accept: application/json  {     "id": "123",     "name": "shampoo",     ...     "ingredients": [         {             "name": "sodium lauryl sulfate"             "links": [                 {                     "rel": "self",                     "href": "/products/123/ingredients/1"                 }             ]         },         {             "name": "sodium laureth sulfate",             "links": [                 {                     "rel": "self",                     "href": "/products/123/ingredients/2"                 }             ]         },         ...     ],     "links": [         {             "rel": "self",             "href": "/products/123"         },         {             "rel": "ingredients",             "href": "/products/123/ingredients"         }     ] } 

having add new ingredient product...

post /products/123/ingredients content-type: application/json accept: application/json  {     "name": "hydrochloric acid" } 

...or delete existing one.

delete /products/123/ingredients/2 

what best way achieve add / remove operation without having source array?

if remove ingredients array product's json representation , leave link still able add new ingredient right away.

remove operation still need source array download asynchronously. in fact see no point in deleting array without having first. how know ingredient try remove inside?

best regards


Comments