A resource in REST is a similar Object in Object Oriented Programming or is like an Entity in a Database. Once a resource is identified then its representation is to be decided using a standard format so that the server can send the resource in the above said format and client can understand the same format.
Resources are typically created by sending a POST request to the parent collection resource. This creates a new subordinate resources with a newly generated id. For example, a POST request to /projects might be used to create a new project resource at /projects/123.
Resources. A resource-oriented API is generally modeled as a resource hierarchy, where each node is either a simple resource or a collection resource. For convenience, they are often called a resource and a collection, respectively. A collection contains a list of resources of the same type.
What is a REST API? A REST API is an application programming interface that conforms to specific architectural constraints, like stateless communication and cacheable data. It is not a protocol or standard.
I am learning REST principles and I have a doubt on working with complex resources.
Let's say we have two resources, Foo and Bar, and for every Foo I must have a Bar. I want to make the dependency of bar from foo clear to developers using my API, so:
1) I will use links from Foo instances to Bar instances, and viceversa
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 templating that shows the dependency from Foo towards Bar (this is just for humans, as URI should be opaque for REST).
/foos --> Foo resource collection
/foos/{foo_id} --> An instance of a Foo resource
/foos/{foo_id}/bar --> The bar instance associated to the foo instance foo_id
So again, there are not bar without a corresponding foo.
Now I wish to create a Foo resource.
POST /foos
{
'name': 'Yet again another foo instance',
}
And let the server to create the corresponding Bar default (or empty) resource, so the next read will give:
GET /foos/2
{
'name': 'Yet again another foo instance',
'rel_bar': '/foos/2/bar'
}
And...
GET /foos/2/bar
{
'name': null, --> Let's say that null is the default value.
'rel_foo': '/foos/2/bar'
}
Is 'RESTfully correct' do this? My concerns are:
My personal thought is that since Bar has no meaning without a Foo, probably yes, I should let the server to create it.
Any idea?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With