For HTTP headers it's quite straightforward: draft-nottingham-http-link-header defines this.
Link: <http://example.com/>; rel="previous"; titile="Previous chapter"
Perl's LWP library automatically parses this headers, and in Ruby there's link_header gem.
Implemented in GitHub API v3.
There's a couple of ways to embed links inside a JSON data structure. Here's the list I can come up with:
{
"id": 1234,
"name": "Bob",
"links": {
"self": "/people/1234"
}
}
Pros:
- Simple. Easy to parse and access in client libraries
Cons:
- Can't include attributes other than
href
. What if I want to includetitle
andtype
for example? - Can't have multiple links with the same
rel
, although i think it is a bad practice to have multiple links with the same relationship anyway.
Facebook Graph API implements this format under connections
if you request the API with ?metadata=1
.
{
"id": 1234,
"name": "Bob",
"links": {
"self": { "href": "/people/1234" },
}
}
Pros:
- Easy to parse, yet extensible and can include other attributes
Cons:
- Can't have multiple links with the same rel. Same as above.
GitHub API v3 implementes this format in some of the data types.
{
"id": 1234,
"name": Bob,
"links": [
{ "rel": "self", "href": "/people/1234" }
]
}
Pros:
- Extensible and straightforward.
Cons:
- A little bit annoying to parse, although it could be still one-line
grep
orselect
in most languages, and you can write a wrapper anyway.
Ruby gem roar implements this format.
Have you seen Jackalope? Stevan's go at it: https://github.com/stevan/jackalope
We use it at II, it works pretty well for providing additional links, as well as providing schemas that are validated against for each endpoint.
It uses the second format (links as an array), which has served us well in the real world after some pretty heavy usage.