I study the principles of REST, and I have doubts about working with complex resources.
Let's say we have two resources: Foo and Bar, and for each Foo I have to have a bar. I want the bar dependency on foo to be clear to developers using my API, therefore:
1) I will use links from Foo instances to Bar instances and vice versa
GET /foos/1 Foo: { 'name': 'Foo instance', 'rel_bar': '/foos/1/bar' } GET /foos/1/bar Bar: { 'name': 'Bar instance', 'rel_foo': '/foos/1', }
2) I will use a URI pattern that shows the dependency on Foo to Bar (this is easy for people, since the URI should be opaque to REST).
/foos
So again, there is no bar without the corresponding foo.
Now I want to create a Foo resource.
POST /foos { 'name': 'Yet again another foo instance', }
And let the server create the appropriate bar by default (or empty) the resource, so the following reading will give:
GET /foos/2 { 'name': 'Yet again another foo instance', 'rel_bar': '/foos/2/bar' }
BUT...
GET /foos/2/bar { 'name': null, --> Let say that null is the default value. 'rel_foo': '/foos/2/bar' }
Is "RESTfully correct" to do this? My problems:
- correctly allow the server to automatically create a linked resource? Or should I separate the creation of Bar and Foo in two steps?
- is a valid POST representation (name attribute only) and returns another ("name" and assigned "rel_foo").
My personal thought is that since Bar doesn't make sense without Foo, maybe yes, I should let the server create it.
Any idea?
Ameba spugnosa
source share