api - RESTful web service: create new resource by combining other resources: provide IDs or URIs? -
api - RESTful web service: create new resource by combining other resources: provide IDs or URIs? -
when obtaining collection of items restful web service (via get), representation of each single item (e.g. in json) contains item's resource identifier. can either id of resource or entire uri contains id.
this identifier (id or uri) required in case client needs farther interact remote resource representing single item. many people seem consider practice provide entire uri , not id, client has nil uri construction (for example, miguel grinberg writes in this article).
but should done in case multiple items combined in order create new resource? client needs tell server items combined. , eventually, server requires list of ids processing request. assuming client retrieved uris each item in first place -- perform uri parsing in order extract raw ids again: in client or in server?
example: client retrieved collection of pages in request. each page item identifies uri (containing id):
{ "pages": [ { "content": "bla bla", "uri": "/pages/1" }, { "content": "that no interesting content", "uri": "/pages/2" }, ... ] }
now assume client instructs server create new resource combining multiple pages: book, built pages 1 , 2. post request body can either contain ids or uris:
{ "title": "a boring book", "pages": [1, 2] }
or
{ "title": "a boring book", "pages": ["/pages/1", "/pages/2"] }
in first case, clients needs know construction of uri , extract id before sending request. in sec case server needs extract id uri.
on 1 hand, thought of resources beingness represented on client side uris only. on other hand, maintain things simple , pragmatic , why should send entire uris server when context clear , ids needed (the book creation not straight deed on page resources)?
what prefer , why? or think not important?
do think next approach compromise? client-side extraction of id uri parsing uri right left , extracting number after rightmost slash, i.e. assuming uri construction without need hardcode entire path.
i think clients should receive absolute urls server , utilize these without kind of modification. therefore, go 1 step farther beyond lastly example:
{ "title" : "a boring book", "pages" : [ "http://.../pages/1", "http://.../pages/2" ] }
only server should responsible extract ids urls if necessary.
api rest uri restful-url restful-architecture
Comments
Post a Comment