json - REST service semantics; include properties not being updated? -
suppose have resource called person
. can update person
entities doing post
/data/person/{id}
. suppose simplicity person has 3 properties, first name, last name, , age.
get /data/person/1
yields like:
{ id: 1, firstname: "john", lastname: "smith", age: 30 }
.
my question updates person , semantics of services this. suppose wanted update john, he's 31. in terms of design approach, i've seen apis work 2 ways:
option 1:
post /data/person/1
{ id: 1, age: 31 }
right thing. implicitly, property isn't mentioned isn't updated.
option 2:
post /data/person/1
full object have been received get
-- properties must specified, if many don't change, because api (in presence of missing property) assume proper value null
.
which option correct recommended design perspective? option 1 attractive because it's short , simple, has downside of being ambiguous in cases. option 2 has sending lot of data , forth if it's not changing, , doesn't tell server what's important payload (only age changed).
option 1 - updating subset of resource - formalised in http patch method. option 2 - updating whole resource - put method.
in real-world scenarios, it's common want upload subset of resource. better performance of request , modularity/diversity of clients.
for reason, patch more useful put in typical api (imo), though can support both if want to. there few corner cases platform may not support patch, believe rare now.
if support both, don't make them interchangeable. difference put is, if receives subset, should assume whole thing uploaded, should apply default properties omitted, or return error if required. whereas patch ignore omitted properties.
Comments
Post a Comment