Let's say I have a collection of books which we'll call a library. GET domain.com/library should return a list of books in the library in HAL compliant JSON. How should I format the JSON? How could I embed the book resources? Here is the format I'm currently thinking about:
{
"books": [
{
"name": "Fight Club",
"_links": {
"self": {
"href": "domain.com/library/Fight-Club"
},
},
...
},
....
],
"_links" : {
"search": {
"href": "domain.com/library/search"
},
...
},
"_embedded" : {
"Fight Club": {
"author": "Chuck Palahniuk",
...
[Same links as above]
}
}
}
The Hypertext Application Language (abbr. HAL) is an open specification that provides a structure to represent RESTful resources. It defines two hypermedia types to extend for XML and JSON.
A collection is a group of JSON documents that exist within one database. Documents within a collection can have different fields, although, generally all documents in a collection have a similar or related purpose.
HAL is a simple format that gives a consistent and easy way to hyperlink between resources in your API. The HAL format is strictly coupled to HATEOAS. The main target of HATEOAS is to decouple the API Consumer from the paths used in the API.
Hypertext Application Language (HAL) is an Internet Draft (a "work in progress") standard convention for defining hypermedia such as links to external resources within JSON or XML code (however, the latest version of HAL Internet-Draft expired on November 12, 2016.).
As the HAL specification was written, the _embedded
object is intended for sub-resources of a given resource. So your bare-bones HAL JSON would look like this.
{
"_links": {
"self": {
"href": "/library"
}
},
"_embedded": {
"item": [{
"_links": {
"self": {
"href": "/library/Fight-Club"
}
},
"author": "Chuck Palahniuk",
"title": "Fight Club"
}]
}
}
The immediate properties of the _embedded
object are link relations. The item
relation is a standard relation that means a resource is an item belonging to the context resource (in this case, your library). You can create custom link relation by using CURIEs.
Notice the lack of a books
array in the top-level object. You may include it if you wish, but it's merely a convenience. A HAL library will only be aware of what you put in _links
and _embedded
. See this discussion regarding collections in HAL and the human factor in deciding where to put your data.
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