I'm trying to understand the concepts of HATEOAS (Hypermedia As The Engine Of Application State) in REST. The following have been very useful:
REST APIs must be hypertext-driven (from Roy Fielding himself)
From them I understand that HATEOAS isn't just about providing links to tell a client what the next valid actions are, it's also about providing information about the valid methods that can be applied to a resource, and the format of the data being passed in both directions between the client and the server.
It's the data format that I'm having trouble with. Roy Fielding mentions defining new media types to describe the data formats. If new media types have to be created for every API anyone creates there would be hundreds of thousands of single-use media types defined.
To get around this issue, someone suggested using the HAL (Hypertext Application Language) media type for RESTful APIs. HAL includes the concept of CURIEs, prefixes for relation names that expand into URLs that point to documentation.
For example,
"_links": {
"curies": [
{
"name": "doc",
"href": "http://haltalk.herokuapp.com/docs/{rel}",
"templated": true
}
],
"doc:latest-posts": {
"href": "/posts/latest"
}
}
Here the relation name is doc:latest-posts
where doc
is a CURIE that expands to http://haltalk.herokuapp.com/docs/
, and http://haltalk.herokuapp.com/docs/latest-posts
will point to documentation about the latest-posts resource (note that the URL to the latest-posts resource itself, as opposed to the documentation about it, is /posts/latest
)
This seems a good way for client-side developers to learn about each resource provided by the API. But does having an automatic link to human readable documentation satisfy the discoverability requirements of HATEOAS? Or must the information describing the data formats required for each resource be machine readable (eg a custom media type for the API)?