HAL is a hypertext format for m2m interaction. It provides the following hypermedia factors:
- Embedded Links (LE)
- Out-bound navigational links (LO)
- Templated Queries (LT)
- Link relations(CL)
HAL specifies two elements:
- link
- resource
Both elements share the following attributes:
- @rel
- @href
- @name
The link and resource elements differ in the following ways:
Link elements..
- The link element is intended for representing out-bound links and should be written with solo/self-closing tags.
- @href value of a link element may contain a URI template to express a templated query link.
Resource elements..
- The resource element is intended for representing the embedded state of other resources and should be written with open and close tags, with the embedded representation contained within.
- The root element must always be a resource with an @rel of self and an appropriate @href value.
Other rules:
- @name must be unique between all HAL elements (link + resource) with the same @rel value in a document, but should not be considered unique within the entire document. This means a link element cannot be referred to by @name alone (thanks to Darrel for this)
- The subject of an @rel is always directed at the closest parent resource element.
e.g. A link that appears within an embedded resource relates to the embedded resource, and not the root resource.
Here is an example:
And here's how that might look in json:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"_links" : { | |
"search" : { "href" : "/todo-list/search;{searchterm}" }, | |
"description" : { "href" : "/todo-list/description" } | |
}, | |
"_embedded" : { | |
"owner" : { | |
"_links" : { | |
"self" : { "href" : "http://range14sux.com/mike"}, | |
"friend" : { "href": "http://mamund.com/" } | |
}, | |
"name" : "Mike", | |
"age" : 36 | |
}, | |
"item" : [{ | |
"title" : "Find Mug", | |
"content" : "Find my mug.", | |
"_links" : { | |
"self" : { "href": "http://home.com/tasks/126" }, | |
"next" : { "href" : "http://work.com/todos/make-some-tea" } | |
} | |
},{ | |
"title" : "Make tea", | |
"content" : "For make drunking goodly tea that is green.", | |
"_links" : { | |
"self": { "href": "http://work.com/todos/make-some-tea" }, | |
"prev" : { "href" : "http://home.com/tasks/126" } | |
} | |
}] | |
}, | |
"created_at" : "2010-01-16", | |
"updated_at" : "2010-02-21", | |
"summary" : "An example list" | |
} |
One comment about uri-templates.
ReplyDeleteMaybe it should be stated somewhere that this is a uri-template, so you don't have to introspect the href attribute to figure that out.
I suggest a new type for that, either as an element like "link-template", or as an attribute href-template="" which MUST be present, which makes the href attribute illegal.
Hi Erlend,
ReplyDeleteThat's a fair point - when I discussed hal with everyone on IRC this was raised as an issue.
My personal preference, if I had to, would be to add a 'link-template' element.
However, I've chosen not to add that for the time being because, afaict, uri-template applicability for a given link can be indicated by the rel (e.g. rel='search'). And, at the moment, I can't envisage any cases where that approach would not suffice.
Cheers,
Mike
hi Mike,
ReplyDeleteThis is really great stuff. I have a couple of questions since I'm pretty new to HAL.
1.- Every response of your RESTful service has a root element "resource"?
2.- How can I tell (or where would that information be) which type of resource it is when using rels like "owner, item, next, prev, self"?
Cheers!
Hi, Is this part of a bigger project?
ReplyDeletei.e. is there somewhere showing this being used?
Right here is the perfect website for everyone who wants to understand this topic. You understand a whole lot its almost tough to argue with you (not that I personally will need to…HaHa). You certainly put a new spin on a subject that has been discussed for decades. Great stuff, just wonderful
ReplyDeleteI am very much pleased with the contents you have mentioned. I wanted to thank you for this great article.
ReplyDeletePremium Whmcs templates