A client gets a list of items from an API. How does a truly RESTful API tell the client where to go next to edit an item without the client knowing the URL beforehand?
A truly RESTful API guides the client using Hypermedia as the Engine of Application State, commonly abbreviated as HATEOAS. When the client retrieves a list of items or a specific item, the server's response for each item, or the collection as a whole, includes not only the data for that resource but also hypermedia controls, which are essentially links. These links explicitly tell the client what actions are available for that specific resource and where to find the corresponding endpoints. For instance, if a client fetches an item, the server's response will embed a link with a descriptive relationship, such as "edit" or "update", and the Uniform Resource Locator (URL) associated with that action. The client then identifies the desired action by its relation (e.g., "edit") and follows the provided URL. This means the client does not hardcode URLs for specific actions like editing. Instead, the client only needs to understand the initial entry point to the API and the meanings of the link relations provided by the server. All subsequent interactions and transitions between application states are driven by the links discovered in the server's responses. For example, a JSON response for an item might look like this: `{"id": "item123", "name": "Product X", "_links": {"self": {"href": "/items/item123"}, "edit": {"href": "/items/item123/actions/edit"}}}`. Here, the client receives the item data and, crucially, a `_links` object containing a link with the relation "edit" and its target URL. The client, knowing it wants to edit the item, looks for the "edit" link and sends a subsequent request, typically a PUT or PATCH, to the `href` specified in that link. This approach ensures a loose coupling between the client and the server, as the server can change its URL structure without breaking the client, provided the link relations remain consistent. The client simply follows the server's instructions.