REST: Resources and Representations

BTI360 Blog / March 23, 2010 in 

Now that Topher figured out how to make an HTTP request to a RESTful Web Service, he needs to know all about resources and representations.

Resources are the objects that a web services exposes to the world. The resources are maintained and controlled by the service.  A resource could be data in a database, a physical object such as a person, or a conceptual object like “the most recent blog posting.”

Clients interact with resources through representations.  And a resource can have many different types of representations.  Examples of common representations are “text/html”, “application/xhtml+xml”, “application/xml”, “application/json”, “image/jpeg”, “image/gif”, and “audio/mpeg”.  RESTful web services can send and accept representations of resources in any of these content-types that the service chooses.

One of the reasons RESTful web services are popular is because they can easily provide many representations for a resource.  Many RESTful web services provide the ability to return the results of a request in multiple formats by specify the format as part of the request.  RESTful services provide a default format if none is specified on the request. Some web service implementations like SOAP only allow one response format.  Lets look at some common response formats for an example

GET request:

GET http://www.bti360.com/dictonary?synonym=happy

From a previous post we know that this request will return synonyms for the word ‘happy’.  Now lets see a few possible representations for this request.  In the following examples note that the response format is specified on the HTTP request.

XML

GET http://www.bti360.com/dictionary/happy/synonyms.xml

The XML representation in the response looks like

<?xml version="1.0" encoding="UTF-8"?>
<synonyms>
    <synonym>chipper</synonym>
    <synonym>glad</synonym>
    <synonym>pleased</synonym>
    <synonym>tickled</synonym>
</synonyms>

JSON

GET http://www.bti360.com/dictionary/happy/synonyms.json

The JSON representation in the response looks like

[{"synonym": "chipper"}, {"synonym": "glad"}, {"synonym": "pleased"}, {"synonym": "tickled"}]}

HTML

GET http://www.bti360.com/dictionary/happy/synonyms.html

The HTML representation in the response looks like

<ul>
    <li>chipper</li>
    <li>glad</li>
    <li>pleased</li>
    <li>tickled</li>
</ul>

CSV

GET http://www.bti360.com/dictionary/happy/synonyms.csv

The CSV representation in the response looks like

chipper,glad,pleased,tickled

As you can see RESTful Web Services allow for many possible representations of the same resource.  They can be standard or you can create your own.  This flexibility allows many types of clients to access RESTful services. However, flexibility can also be dangerous. Whenever possible it is recommended that a RESTful web service use standard representations for their resources.  This allows clients to quickly and easily integrate with your service.

Previous

REST: HTTP Requests

Next

RESTful Content Negotiation

Close Form

Enjoy our Blog?

Then stay up-to-date with our latest posts delivered right to your inbox.

  • This field is for validation purposes and should be left unchanged.

Or catch us on social media

Stay in Touch

Whether we’re honing our craft, hanging out with our team, or volunteering in the community, we invite you to keep tabs on us.

  • This field is for validation purposes and should be left unchanged.