What does “state transfer” in Representational State Transfer (REST) refer to?
Asked Answered
W

2

22

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?

Waterlogged answered 5/1, 2011 at 11:42 Comment(2)
see this response at the link https://mcmap.net/q/236070/-what-does-representational-state-mean-in-restLeatherman
Try also the answer at this link: https://mcmap.net/q/242604/-in-what-do-consist-the-misunderstandings-about-the-quot-rest-quot-word-and-its-meaning-closedLeatherman
L
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.

Linton answered 5/1, 2011 at 11:46 Comment(13)
@S.Lott: What about the behavior? Does REST address that too or the behavior is not something of interest about the resource?Waterlogged
Despite how it sounds, State Transfer is not at all the same as object serialization.Saphead
@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.Linton
@Waterlogged 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.Saphead
@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.Linton
@Linton 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.Saphead
@Darrel Miller: The client application just transition is not part of REST. That's irrelevant.Linton
@Linton 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?Saphead
@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.Linton
@Linton Yes, normally it is a user that clicks on hyperlinks and submits forms. I'm quite sure I didn't say anything different.Saphead
@Darrel Miller: "State transfer is more about transitioning from one state in a state machine to another" was hard to understand.Linton
@Linton I should have clarified that the state machine I was referring to was one on the client.Saphead
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?Crossbred
S
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.

Saphead answered 6/1, 2011 at 0:27 Comment(3)
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?Crossbred
@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.Saphead
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 #27016814 , I will appreciate it if you can take a minute to answer there! thank you!Crossbred

© 2022 - 2024 — McMap. All rights reserved.