HATEOAS

What is HATEOAS?

HATEOAS stands for the phrase Hypermedia As The Engine Of Application State.

What problem does HATEOAS solve?

When we build an application with REST APIs unknowingly we tend to tightly couple frontend with backend.

Consider this case of fetching a user.

GET http://localhost:8080/users/1

It is common to see the REST API responding with something like:

{
"id": 1
"name": "John Doe"
"email": "john@example.com"
}

But, in the the client, we might see a button to see the users address. Clicking on this button would make the below API call.

GET http://localhost:8080/users/1/address

Now, Why should the frontend know the address URL?.

This is the tight coupling problem and what HATEOAS solves.

The idea behind HATEOAS

A REST API should return a complete document. This means the clients have all the data required to show the document and the actions associated with that document.

The solution - Hypertext Application Language (HAL)

HAL is a way to structure the the data the associated links in JSON or XML response. HAL specification by IETF is currently in a draft stage.

Example HALs

{
"id": 1
"name": "John Doe"
"email": "john@example.com"
"_links": {
"self": {
"href": "http://localhost:8080/users/1"
},
"address": {
"href": "http://localhost:8080/users/1/address"
}
}
}