Solved

Understanding Representational State Transfer - REST

Posted on 2010-08-20
6
767 Views
Last Modified: 2013-11-10
Hi I would like someone's easy to understand basis definition of REST.

I cannot get my head around the Wiki definition.

Once I can grasp the concept, great, I can go more indepth.

Thanks anyone
0
Comment
Question by:Johny_Brav0
6 Comments
 
LVL 6

Expert Comment

by:Neeraj Soni
ID: 33533296
consider this...

REST -  approach for getting info/data from a Web using a designated URL that will return an XML file/streamed text  that describes and includes the content
a web page or a web service is the representation of resource

web method is a verb, e.g. get, put, post, delete etc.
class is noun, e.g. members, friends, articles

you call the verb on nous using url and get the state of a resource which you can represent in your way. the is generally be in xml form so that representation can be anything.

you can get details of your friend and use that in any way.

If you are aware of RPC, you would have getUser().
Same is implemented using REST using a URL like this http://example.com/users/
and hence the client would call it like this
user = new Resource("http://example.com/users/ABC")
 user.Get()

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 33553020
Maybe a simple code example would help.  The code snippet contains a "web service" implemented in PHP.  The service takes two inputs: an API key and a first name.  It responds with a short string giving the last name.  Try using it with these URLs and you can see the moving parts.

http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray
http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Richard
http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Fred
http://laprbass.com/RAY_REST_get_last_name.php?key=XYZ&name=Ray

Other good examples of a RESTful web service include the popular Yahoo and Google geocoders.

In a RESTful environment, each request is atomic and complete, and each response is complete.  The calling program does not need to know anything about what the service is doing - it just gets the answer.  Contrast SOAP, where the interface is a complex "contract" between the entities.  In the example below, the information could come from a data base, or even from another web service, but you do not need to care about any of that - you just ask and receive.  It really is that simple.

REST services can return anything.  XML is common, as is JSON, but so are CSV strings, or just a plain string of text.  There is no "law" about it, just convention.

REST services are commonly used as the "behind-the-scenes" actors in AJAX web sites.

Does that help clear it up?  If not, please post back with any specific questions and I will try to help. ~Ray
<?php // RAY_REST_get_last_name.php
error_reporting(E_ALL);



// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray');



// OUR DATA MODEL CONTAINS ALL THE ANSWERS
$dataModel
= array
( 'Brian'   => 'Portlock'
, 'Ray'     => 'Paseur'
, 'Richard' => 'Quadling'
)
;


// TEST THE API KEY - THE ASSUMPTION IS FAILURE
$key = FALSE;

// IF THE URL CONTAINS THE KEY
if (isset($_GET["key"])) $key = $_GET["key"];

// IF THE KEY FAILS TO MATCH OUR OUR REQUIRED 'ABC'
if ($key !== 'ABC') die('BOGUS API KEY');



// LOOK UP THE LAST NAME - THE ASSUMPTION IS FAILURE
$name="?";

// IF THE URL CONTAINS THE FIRST NAME
if (isset($_GET["name"])) $name = $_GET["name"];

// IF THE URL NAME IS FOUND IN THE DATA MODEL
if (array_key_exists($name, $dataModel))
{
    // RETURNS THE LAST NAME FROM THE DATA MODEL
    die("$dataModel[$name]");
}

// RETURNS THE UNKNOWN NAMWE INDICATOR
else die('UNKNOWN');

Open in new window

0
 

Author Comment

by:Johny_Brav0
ID: 33563055
Ray I liked your answer but I am still vague on REST - so REST is a simple get/send concept i.e. specify values using GET method in your example above and return values depending on this. Unlike SOAP which is a complex contract - how is SOAP more complex?

I think If I had a small amount of advantages and disadvantages for SOAP and REST I would understand completely.

Cheers
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33565140
Probably the best answer I can give you is the practical one.  I see hundreds of questions here at EE from people who are confounded by SOAP interfaces.  I never see one from somebody who does not understand a REST interface.  That ends the argument for me!

The Wikipedia article is not light reading - it requires some research and computer science background - but I have read it and I think it lays out the basics fairly well.
http://en.wikipedia.org/wiki/Representational_State_Transfer

The Stack Overflow discussion is pretty good.
http://stackoverflow.com/questions/76595/soap-or-rest
0
 
LVL 6

Expert Comment

by:Neeraj Soni
ID: 33565508
Comparison here:
http://www.ajaxonomy.com/2008/xml/web-services-part-1-soap-vs-rest

A good presentation:
http://www.slideshare.net/cesare.pautasso/rest-vs-soap-making-the-right-architectural-decision-1st-international-soa-symposium-amsterdam-october-2008-presentation

Extract from the site:
To summarize their strengths and weaknesses:
SOAP:Pros:
    * Langauge, platform, and transport agnostic
    * Designed to handle distributed computing environments
    * Is the prevailing standard for web services, and hence has better support from other standards (WSDL, WS-*) and tooling from vendors
    * Built-in error handling (faults)
    * Extensibility

Cons:
    * Conceptually more difficult, more "heavy-weight" than REST
    * More verbose
    * Harder to develop, requires tools

REST:Pros:    * Language and platform agnostic
    * Much simpler to develop than SOAP
    * Small learning curve, less reliance on tools
    * Concise, no need for additional messaging layer
    * Closer in design and philosophy to the Web

Cons:
    * Assumes a point-to-point communication model--not usable for distributed computing environment where message may go through one or more intermediaries
    * Lack of standards support for security, policy, reliable messaging, etc., so services that have more sophisticated requirements are harder to develop ("roll your own")
    * Tied to the HTTP transport modelComparison here:
http://www.ajaxonomy.com/2008/xml/web-services-part-1-soap-vs-rest
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 33613298
About the following sample url:
http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray

I would say that is totally against REST recommendations:
Countersense to SOAP, REST should not mention verbs (like get_last_name) in the URI. You should specify a resource.

This is more REST compliant:
GET http://yourdomain.com/REST.svc/persons  -> Get a list of persons
GET http://yourdomain.com/REST.svc/persons/A123  -> Get an specific person with ID A123
GET http://yourdomain.com/REST.svc/persons?lastname=Doe  -> Get list of persons with lastname=Doe

The resources are named in the URI, as "persons" or "A123". This is the trick of REST: name a resource and get the data (usually XML or JSON, but can be other).
Also you have POST, PUT and DELETE operations with similar style.
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web design and cms 1 185
Online Business Portal 3 59
Creating a WSDL file using wsdl.exe 1 95
How to call a web service and get the results posted in a form in PHP 9 53
When coding a Webservice to provide data, the normal way is to create a method that returns a Dataset object.   But a Dataset cannot be used easily from another platform, such as PHP,  FLAS (FLEX) , etc. Solution : Return a XML Document instea…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question