21

What does the State Transfer in Representational State Transfer refer to?

Found some explanations about this (e.g. here) but I still don't understand. For example in the article it is said

The representation places the client application in a state.

Why? What does state (as I understand it, something like a session) have to do with a representation of a resource?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
user2011
  • 311
  • 3
  • 5

2 Answers2

21

Why? What does state (as I understand it, something like a session) have to do with a representation of a resource?

An object has attributes (or state) and behaviors (or methods).

If I want to move an object from my desktop to a server I have to do the following:

  1. Create a representation of the state of the object.

  2. Transfer that representation from the desktop to the server.

The methods I don't transfer. I install the same class definition on both machines.

So, REST is about creating a representation of the object's current state so it can be transferred to another server from which the object can be reconstructed.

We only send the state -- the attributes -- of the object. And we have to create an external, serialized representation of that state.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • @S.Lott: What about the behavior? Does REST address that too or the behavior is not something of interest about the resource? – user2011 Jan 05 '11 at 13:43
  • 3
    Despite how it sounds, State Transfer is not at all the same as object serialization. – Darrel Miller Jan 05 '11 at 14:02
  • @user2011: "What about the behavior?" REST doesn't address behavior. It's "state transfer". The current state of the object is what's represented. Behavior is a matter of proper configuration control at each end. – S.Lott Jan 05 '11 at 16:53
  • 4
    @user2011 State transfer is more about transitioning from one state in a state machine to another. REST "state transfer" is all about behaviour and nothing to do with transfering objects across the wire. – Darrel Miller Jan 05 '11 at 23:33
  • @Darrel Miller: "State transfer is more about transitioning from one state in a state machine to another". Doesn't explain the idempotent GET request very well. – S.Lott Jan 06 '11 at 01:00
  • @S.Lott GET /Home => GET /About The client application just transitioned from the "Home" state to the "About" state. Nobody said a state transition needs to change resource state. – Darrel Miller Jan 06 '11 at 01:13
  • @Darrel Miller: The client application just transition is not part of REST. That's irrelevant. – S.Lott Jan 06 '11 at 01:34
  • @S.Lott The REST hypermedia constraint, aka Hypermedia as the Engine of Application State, is specifically referring to the state of the client. Did you read my answer to this question? – Darrel Miller Jan 06 '11 at 02:04
  • @Darrel Miller: I couldn't understand your answer. REST is state transfer. The client's state depends on user actions, not server actions. The client state machine is driven by the human user. Unless -- of course -- you're talking about something else. – S.Lott Jan 06 '11 at 02:21
  • @S.Lott Yes, normally it is a user that clicks on hyperlinks and submits forms. I'm quite sure I didn't say anything different. – Darrel Miller Jan 06 '11 at 02:31
  • @Darrel Miller: "State transfer is more about transitioning from one state in a state machine to another" was hard to understand. – S.Lott Jan 06 '11 at 02:32
  • @S.Lott I should have clarified that the state machine I was referring to was one on the client. – Darrel Miller Jan 06 '11 at 02:49
  • 1
    1. So REST is to avoid saving state , instead, we transfer the state each time the client talks to the server? 2 Does it I have to store the state to the client side and put it into the request so that the server can get and then transfer another state back? 3.What about I store state in db or memcache? Is this a kind of REST? – JaskeyLam Nov 19 '14 at 11:28
13

Consider a client application to be a kind of giant state machine. The client's initial state is equal to the first representation returned from the server. Links in the returned representation provide possible "state transitions".

One thing to note is that there are a two major types of links, passive and active. Passive links like <img> and <link rel="stylesheet"> do not actually cause a state transition, they simply augment the current state. Links like <form> and <a> however are active links and the cause a state transition. After following one of these links the new client state is equal to the returned representation, aka state transfer.

If you are used to building traditional desktop client applications you will find this is a radically different architecture. Not one that you are likely to grok overnight. Initially this approach may seem very limiting but when you consider that a client application can be the host to many simultaneously executing state machines you will start to realize that you can do just about anything that you could using a remote object architecture and still maintain the loose coupling of web browser.

Løiten
  • 3,185
  • 4
  • 24
  • 36
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • 1
    Does Transfer the same as transition? Can I think that ST(transfer) means we transfer client state each time instead of storing it into a local macine? – JaskeyLam Nov 19 '14 at 11:24
  • 1
    @Jaskey The transition is the effect of doing a transfer. You might be transferring client state to the server and/or transferring resource state from the server to the client. – Darrel Miller Nov 20 '14 at 14:14
  • So, another question, if I use memcache or db to store user state like "is loggedin", is this definitely not a REST application? I saw many posts or videos that we must let client be responsible for its own state, and no state will be stored in server. But many posts stated that, to make a stateless application, we can use db or memcache to store the state,which confuses me now, so I ask a question http://stackoverflow.com/questions/27016314/does-ststate-transfer-in-rest-mean-that-state-must-be-held-by-client , I will appreciate it if you can take a minute to answer there! thank you! – JaskeyLam Nov 21 '14 at 02:36