andrzej541
andrzej541

Reputation: 961

HATEOS provide by server or client?

This is my first contact with HATEOS. I make a frontside client application which has to be connected with server using this format. I saw a JSON pattern with all hyperlinks refers to related objects.

My question is should I created those hyperlinks from client side app or this is the task for the server?

Upvotes: 1

Views: 45

Answers (1)

David Brabant
David Brabant

Reputation: 43509

Links are generated on the server side.

You must distinguish between application state, and the state of a resource on a server. Clients handle application state. Servers handle resource state.

An application state determines “where” the client is in the process of completing a task, i.e.: where it is in its interaction with the server.

A resource state is the kind of persistent data a server stores, and lasts beyond the duration of a single session of interactions.

Along with the representation of a resource itself, comes some metadata (the “links” section in the JSON representation) telling the client what can be done next with that resource. To actually alter the resource state, the client has to follow one of the provided links, generated by the server.

Let’s take a concrete example that illustrates these concepts.

If you are browsing through a thumbnail gallery and the server has just sent you thumbnail number 23, your client cannot simply say “next” to the server, because this would imply that the server has to remember that your client application had asked for number 23 previously. And it would have to keep that “context” for each connected client. If instead, the client explicitly asks for thumbnail number 24 to advance in the gallery, the server is relieved from that burden: between two successive calls to your thumbnail service, the server hasn’t to remember anything. The “context” (i.e. the relevant part of application’s state) is kept on the client and explicitly passed to the server.

The good thing is that you don’t necessarily have to know either that you were viewing that specific thumbnail in the previous call. Because, along with the representation of this image, the server can send you links labeled “previous” and “next” leading to the corresponding thumbnails. At the time the server was generating the representation of thumbnail 23, the server had all information related to your request. It knew what thumbnail you requested, and what the previous and next ones were. It can therefore generate the corresponding links in the HATEOAS representation.

Upvotes: 2

Related Questions