Sobes.tech
Middle+

Explain the rule of a uniform interface.

sobes.tech AI

Answer from AI

The Uniform Interface rule is one of the six architectural constraints of REST. It prescribes that the system use a single, standardized approach to interaction between the client and server. This simplifies and decouples the architecture, improves visibility, and facilitates independent development of the client and server.

Components of the Uniform Interface:

  1. Identification of resources: Resources must be uniquely identifiable (usually via URI). This is the basis for standardized interaction. Example: /users/123, /products/ABC.
  2. Manipulation of resources through their representations: The client receives a representation of the resource (e.g., in JSON or XML format), which contains enough information to modify or delete the resource on the server. The resource's state on the server can be changed by sending the modified representation.
  3. Self-descriptive messages: Each message (request or response) must contain enough information to be fully understood without the context of server state or other requests. This includes metadata: content type, caching information, links to other resources, etc.
    HTTP/1.1 200 OK
    Content-Type: application/json
    Cache-Control: max-age=3600
    
    {
      "name": "Example User",
      "links": [
        {
          "rel": "self",
          "href": "/users/123"
        }
      ]
    }
    
  4. Hypermedia as the Engine of Application State (HATEOAS): Allows the client to dynamically navigate between resources based on hypermedia links provided by the server in the response. The client should not need to know all URIs in advance except for the entry point. The server guides the client through available actions and resources.
    {
      "orderId": "A123",
      "status": "processing",
      "links": [
        {
          "rel": "cancel",
          "href": "/orders/A123/cancel",
          "method": "POST"
        },
        {
          "rel": "self",
          "href": "/orders/A123"
        }
      ]
    }
    
    Instead of hardcoded paths, the client learns about the possibility to cancel an order by the presence and content of the link with rel: "cancel".

A consistent interface is critically important for the scalability and evolvability of RESTful systems, as it reduces coupling between components.