tags:

views:

151

answers:

5

What is the real meaning of Resources with multiple representations for the restful? After reading InfoQ's "A Brief Introduction to REST", I am confused. What is Representations?

+1  A: 

A Resource is basically a collection of data, in the example it is the associated data with a given customer.

When you retrieve a resource, you get a representation of it. Now for most data there are multiple representations available. Think of a table of data, or a chart, etc... In the example you define which representation you would like to receive by setting the HTTP Accept header. In the first example in an xml format, in the second one in a vcard format.

Zed
Does the client decides which way they will negotiate? Does server need to do something? For example If client wants to xml representation, does the server had to do something, Or resource is enough for deal.
Iguramu
Well, the server has to generate the xml representation from the available data, and send it back to the client.
Zed
is it the same thing that all the webservices do. I mean "representation". If not how does others communicate?
Iguramu
Some representations define the format, others only the scheme. Taken the example, vcard is a well defined structure. Given the same customer, the received representation (vcard file) should be identical from all servers. XML on the other hand only gives you a scheme, nothing more, so XML representations may vary from server to server; you can only be sure that you can parse it as an XML structure.
Zed
@Zed exactly, which is why XML alone is not self descriptive, a violation of REST pretty much
Wahnfrieden
Actually the examples are pretty neat on the page linked in the question. There the accept header is set to "application/vnd.mycompany.customer+xml".
Zed
A: 

Take a look at this: REST Wikipedia article

A resource is something on the server, a "thing", and the article is just saying you can have multiple message formates returned about that "thing" that describe it in different ways...

Peter Short
If i change that "thing", does it means i change resourse? If not, how can i change resourse?
Iguramu
Please don't reference the Wikipedia article, it's in a dismal state and doesn't rely on authoritative sources.
Wahnfrieden
+5  A: 

A representation is a certain way to display and/or transfer data. The same resource can be represented in different ways:

  • As HTML page
  • As an XML document
  • As a JSON data structure
  • As plain text
  • Even as a PDF file if that would be desired
  • ...

You can exchange "representation" with "data format" to get a better understanding.

Examples for a "customer" resource:

HTML:

<h1>John Doe</h1>

XML:

<customer-name>John Doe</customer-name>

JSON:

{
  "UserName" : "John Doe",
}

A metaphor:

Just think of a picture. It can be represended as Bitmap, PNG, JPEG and many other formats and data structures. All of them show the same picture but they differ in their internal structure. (their "representation")

Practical considerations:

In a web application environment the most common representation is (X)HTML as the standard output sent to the browser. Followed by XML and JSON when it comes to Ajax and automated access to the web application.

DR
i got it now. If we have sources, we need to represent them somehow. But new question is why don't we use real sources? that is because of makes server so slow?
Iguramu
No, on the contrary, adding a representation layers has nothing to do with caching or any other speed optimization. In most cases you cannot just send the original source. When your data comes from a MySQL database, what would you send? You just *have to* convert your data into *some* kind of representation.
DR
if you can check out the last paragraph of http://tomayko.com/writings/rest-to-my-wife . Ryan says that "Nouns aren’t universal and verbs aren’t polymorphic" If nouns means things why aren't they universal? isn't XML a universal data definition language?
Iguramu
To amplify DR's point, even the 'real source' is just another representation - it happens to be the representation you have on disk, but it's still a representation.For more on this idea, see 'The Republic', Plato, book VII 514a–520a.
Tom Anderson
FYI that's the Cave Allegory (http://en.wikipedia.org/wiki/Allegory_of_the_cave)
DR
It's actually frowned upon to have many representations of the same resource. It's usually unnecessary. Also, just using an XML media type for example isn't sufficient for knowing how to process the response and isn't really RESTful. See AtomPub, which uses the "application/atom+xml" media type. Use something like "application/vnd.mycompany.foobar+xml" and document your media types.
Wahnfrieden
If i can't reach "real resource" then how can i change resource? If i change representation then does that change resource?
Iguramu
You do not modify a resource using it's representation. You modify it using the methods (aka "verbs") available for that resource, like POST, PUT and DELETE.
DR
That means when i use verb "POST", I don't call a representation. I call the real one. Is that true?
Iguramu
Yes. POST, PUT and DELETE have nothing to do with representations, they effect the underlying resource (and because of that indirectly the representations, too)
DR
No, you still interact with just a representation. The way these requests actually modify the underlying resource is up to you. For example, you could have a JPG image resource, but a representation of it as a bitmap grid of RGB pixels - the client could PUT a grid of pixels as an array or something, and your service would encode that into JPG and write it over the existing resource.
Wahnfrieden
Just to clarify: Wahnfrieden of course is right, from a client's perspective. I've written my comment from the perspective of the web application, and that application modifies the resource, as Wahnfrieden described it.
DR
how can it be possible writing a bitmap grid of RGB in order to JPG image. How can our database accept that situation. Can i write integer to field of string, using representation.
Iguramu
@Iguramu presumably you would have your web service use a JPG compression tool to convert the data, before pushing it to your database. Web requests don't go directly into your database... You have the opportunity to translate things beforehand.
Wahnfrieden
A: 

Have a look at Roy Fielding's dissertation which defines REST.

Wahnfrieden
I almost read. But he says that "All REST interactions are stateless". I am agree with that. So why the name of REST has "state transfers"? What he really wants to say us when he says "state transfers" even if it is stateless ?
Iguramu
The interaction itself is stateless. Processing each request and response does not require any knowledge of state. The server does not maintain any state between requests. However, the client can maintain state, transparent to the rest of the entire stack.
Wahnfrieden
A: 

Actually "representation" is more abstract than these answers suggest. "Representation" simply means what you get back is not necessarily the entire resource. For example, I have an employee record which is a resource in my corporate HR database. "Employee" is an obvious resource noun to expose through a RESTful architecture. But if you access my employee ID through the e-mail URI, the representation will be entirely different than the representation you see when accessing my employee ID through the HR benefits URI.

What DR's answer describes (JSON, XML, etc.) are actually called media-types in REST terminology. It is simply the data format of the response.

McGuireV10