Search code examples
resthateoas

Links in REST response


In the HATEOAS paradigm, what do links in a REST response signify, an action or a resource? I have an order-form that has a type dropdown. Some advanced options fields are to be loaded in the form based on those selections.

Going by the HATEOAS paradigm, the client should not be expected to know or guess where to load those extra fields from. Hence, I included the 1 link for every option provided. The rel attribute on the link object is supposed to provide some kind of documentation about the intention of the link. Is this a correct implementation ?

The popular coffee shop example available on 'net (in Ian Robinson's talk and an article at InfoQ) uses links to identify next available state transitions. Are these equivalent in spirit?


Solution

  • I wouldn't say that your example is correct or incorrect but rather that it is a compliant hypermedia link. There are different types of hypermedia links and you just need to find the most appropriate. For example, it sometimes makes sense to separate the link relation to a resource from the linked resource. Using XML, you could do this:

    <link rel="type1SpecialOptions" href="http:/yourDomain/specialOptions/type1" />
    <link rel="type2SpecialOptions" href="http:/yourDomain/specialOptions/type2" />
    

    instead of the still valid hypermedia link style:

    <link specialOptions="http:/yourDomain/specialOptions/type1" />
    <link specialOptions="http:/yourDomain/specialOptions/type2" />
    

    Pick a type of link type that makes sense for your application. This article on hypermedia links has a very brief explanation of the types of hypermedia links. It pretty much assumes you are familiar with the terminology of hypermedia and REST so look at the examples first and then follow the links for the different link types to get a better understanding of what is possible.