bitbonk
bitbonk

Reputation: 49619

HATEOAS with JSON over HTTP

I have a simple CRUD-like Web-API using JSON over HTTP (a repository of Person entities, a contacts app). Now I would like to design it in away that it fits well into a RESTful architecure. It's all pretty straight forward, except one thing:

How can I fulfill the HATEOAS constraint if I don't have html? What are good, establishes ways to do HATEOS if the main (currently the only) content type is JSON that just contains the actual entities behind a resource/url?

One thing that comes to mind is the link HTTP header, but is this really the way to go?

Upvotes: 1

Views: 815

Answers (2)

Orri
Orri

Reputation: 919

Don't complicates things with special media types. In my master's thesis I took advantage of the link header to connect to other related resources. In addition, I added Hyperlinks inside json using combination of a URL relation type and URL http link. Json is not a hypermedia format but it can be used as one. See JSON mime : vnd.collection + JSON.

Best advice I can give is to use link header as much as possible until the community decide how to implement hateoas in APIs. Use already available relation types (microtype) and define your own.

Upvotes: 1

Jonathan W
Jonathan W

Reputation: 3799

While plain JSON doesn't afford you link relationships, there are other media types that build off of JSON that do(much the way Atom builds of XML). My personal favorite is the Hypertext Application Language (HAL).

The way I understand Link headers, they're supposed to be used for media types that really don't afford links... things like images and other non-text resources. While you can use Link headers for text-based resources, you don't have the same flexibility to target individual parts of the resource; you're pretty much stuck to linking to / from the entire resource.

Upvotes: 3

Related Questions