Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3694

HAL and HAL-FORMS

$
0
0

I have a problem understanding something. I would like to implement HATEOAS and move to level 3 of a REST API. For now, I am using Spring HATEOAS, which is not very complicated, and it generates responses in HAL format.

The issue is that I want to use the HAL format efficiently on the front-end and use the URIs without duplicating my logic, using the links as a workflow. However, in HAL, there is no concept of method (POST, GET, etc.). On the other hand, I can easily add a "method" field to the response.

Example HAL response:

{"_links": {"self": {"href": "/api/resource/1"    },"update": {"href": "/api/resource/1","method": "PUT"    },"delete": {"href": "/api/resource/1","method": "DELETE"    }  },"id": 1,"name": "Resource Name"}

In this example, I added a "method" field to indicate the HTTP methods (PUT, DELETE) associated with the links.

I then discovered that HAL-FORMS exists, but it is still in draft form. However, I do not understand the advantage of placing actions in _templates when we could have done everything in _links.

Example HAL-FORMS response:

{"_links": {"self": {"href": "/api/resource/1"    }  },"_templates": {"default": {"method": "GET","contentType": "application/json"    },"update": {"method": "PUT","href": "/api/resource/1","contentType": "application/json","properties": [        {"name": "name","required": true,"type": "text"        }      ]    },"delete": {"method": "DELETE","href": "/api/resource/1"    }  },"id": 1,"name": "Resource Name"}

In this example, HAL-FORMS uses a _templates object to define possible actions, such as fetching the resource (GET), updating it (PUT), and deleting it (DELETE). Each action is described with its properties and HTTP methods.

In summary, I would like to understand why HAL-FORMS chooses to separate actions in _templates rather than including them directly in _links. My goal is to find the best way to use these formats to make my REST API more intuitive and efficient for the front-end.


Viewing all articles
Browse latest Browse all 3694

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>