http - Should I allow sending complete structures when using PUT for updates in a REST API or not? -


i designing rest api , wonder recommended way handle updates resources be. more specifically, allow updates through put on resource, should allow in body of put request?

  1. always complete structure of resource?
  2. always subpart (that changed) of structure of resource?
  3. a combination of both?

for example, take resource http://example.org/api/v1/dogs/packs/p1. get on resource give following:

request: http://example.org/api/v1/dogs/packs/p1 accept: application/xml  response: <pack>   <owner>david</owner>   <dogs>     <dog>       <name>woofer</name>       <breed>basset hound</breed>     </dog>     <dog>       <name>mr. bones</name>       <breed>basset hound</breed>     </dog>   </dogs> </pack> 

suppose want add dog (sniffers basset hound) pack, support either:

request: put http://example.org/api/v1/dogs/packs/p1 <dog>   <name>sniffers</name>   <breed>basset hound</breed> </dog>  response: http/1.1 200 ok 

or

request: put http://example.org/api/v1/dogs/packs/p1 <pack>   <owner>david</owner>   <dogs>     <dog>       <name>woofer</name>       <breed>basset hound</breed>     </dog>     <dog>       <name>mr. bones</name>       <breed>basset hound</breed>     </dog>     <dog>       <name>sniffers</name>       <breed>basset hound</breed>     </dog>   </dogs> </pack>  response: http/1.1 200 ok 

or both? if supporting updates through subsections of structure recommended, how handle deletes (such when dog dies)? through query parameters?

yes, send complete representation of resource. otherwise, (according common definition , usage of put) replace pack 1 dog.

however, might want consider following:

  • use xml namespaces or other means of versioning (e.g. ?version=2) outside of url path
  • post dog want add /dogs/packs/p1. post definition creates subordinate resource, add dog pack.
  • refurbish urls bit. seems me want have /dogs/1234, /dogs/1235 , forth, , /packs/p1, /packs/p2. post <dog id="1"> pack, too.

keep in mind rest requires identify resources. packs not subordinate resource dogs, , each dog should have sort of unique identifier. then, when accessing /packs/p1/1234, want redirect /dogs/1234. or, alternatively, not make url available, despite accepting posting of subordinate resources respective pack.

the more think it, more sense post approach makes. maybe have /packs/p1/dogs/ resource dogs, separate pack. then, can put stuff owner information etc /packs/p1, list of dogs via /packs/p1/dogs/ (which should contain list of urls each dog in pack, e.g. /packs/p1/dogs/1234, see hateoas), add new dog pack posting /packs/p1/dogs/, , remove dog deleteing /packs/p1/dogs/1235. each dog either full representation, maybe redirect /dogs/1234 etc, or different representation of dog in context of pack, again link "full" dog. depends on how want represent single dog in pack, , of course influence what post /packs/p1/dogs/. full dog feels wrong, should id showed above, maybe additional data pertaining relationship pack.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -