jsonrestrestful-architecture

What is "representation", "state" and "transfer" in Representational State Transfer (REST)?


I came across a few resources regarding REST but I'm not able to understand things clearly. It would help me if someone could explain things with respect to my example below.

I have a table named User

User table Content

id name
1  xxx

The URL I will be calling is /test/1

The result will be in JSON format, eg: { 1: "xxx" }

My understanding so far regarding REST:

Please do let me know if my understanding is correct. Else, please answer the below questions:


Solution

  • REST is about resource state manipulation through their representations on the top of stateless communication between client and server. It's a protocol independent architectural style but, in practice, it's commonly implemented on the top of the HTTP protocol.

    When designing REST over HTTP, URLs are used to locate the resources, HTTP methods are used to express the operations over the resources and representations such as JSON and/or XML documents are used to represent the state of the resource. HTTP headers can be used to exchange some metadata about the request and response while HTTP status code are used to inform the client regarding the status of the operation.


    What is a resource in my example?

    Understand resource as the concept of a user. Don't think about the table in your database, think about an abstraction of a user with their set of attributes.

    What is a representation in my example?

    A JSON document can be used to represent the state of a particular resource. A resource can have many representations, such as JSON and/or XML documents, and the client can use content negotiation to request different representations of the same resource.

    What is a state transfer or when does this happens in my example?

    The state of a given resource can be retrieved and manipulated using representations.

    A GET request, for example, allows you to retrieve a representation of the state of a resource, sent in the response payload. A PUT request, for example, allows you to replace the state of a resource with the state defined by the representation enclosed in the request payload.


    Example

    Consider a user resource with attributes such as id and name stored somehow in your server:

    These details make the state of the resource.

    A URL such as /users/1 can be used to locate the resource in your server.

    Requests such as GET, PUT and DELETE can be performed against this URL to retrieve/manipulate the state of the resource using representations, such as JSON and/or XML documents (other representations can be supported according to your needs):

    {
      "id": 1,
      "name": "John Doe"
    }
    
    <user>
      <id>1</id>
      <name>John Doe</name>
    </user>
    

    The above shown documents are not the resource itself. They are just a way to represent the resource. which is stored somehow in your server.