This presentation has been developed in the context of the Mobile Applications Development course, DISIM, University of L'Aquila (Italy), Spring 2016.
http://www.ivanomalavolta.com
5. REST Architectural Style
REST is neither a technology nor a framework
REST is an ArchitecturalStyle
à a set of principles + constraints
Those constraints help us in developing applicationsthatare
“easy” to maintainand extend
6. REST Main Constraints
A RESTful system
• is client-server
• is stateless
– there should be no need for the service to keep users’
sessions
– each request should be independent of others
• has to support a caching system
• has to be uniformlyaccessible
– each resource must have a unique address and a validpoint
of access
7. The (static) Web as a RESTful system
1. you type a URL into your browser to reach a specific HTML
page
2. the browser gets and displays the elements of the HTML page
à the browser is getting a representationof the currentstate
of that resource
8. REST Main Actors
These are the abstractionsthatmake a RESTful system:
• Resources
• Representations
• Actions
10. Resources
A resource is “everything”the service can provide
Statesand functions of a remote applicationarealso considered
as resources
Example of resources:
• titleof a movie from IMDb
• a Flash movie from YouTube
• images from Flickr
• order info from eBay
• etc.
11. Resources
In general, a RESTful resource is anythingthat is addressable
over the Web
Addressable = anythingthatcan be accessed and transferred
between client and server
à a resource must have a unique address overthe Web
Under HTTP these are URIs
12. URIs
UniformResource Identifier
in a RESTful web service is a hyperlinkto a resource
It is the only means forclients and servers to exchange
representationsof resources
ex.
.../orderinfo?id=123
13. URIs
The URI is not meant to change over time
à it is the only means to identify a specific resource
URIs are also used to negotiaterepresentations of a given
resource
In the URI you give certainparameters thatdefine which
informationyou wantthe server to returnto you (just like
giving GET variablesto a page)
The server will respond with a resource representation
containingthe information you’veasked
16. URL
UniformResource Locator
A URL is a specializationof URI that defines the networklocation
of a specific resource
Unlike a URI, the URL defines how the resource can be obtained
es.
http://some.domain.com/orderinfo?id=123
18. Representations
The formatof the representationis determined by the content-
type
The interactionof the representationon the resource is
determined by the action (GET, SET,etc.)
19. Content-types
Since we are using HTTP to communicate,we can transferany
kind of informationthatcan be passed between clients and
servers
ex. text files, PDF documents, images, videos, etc.
In anycase, the data is streamed over TCP/IPand the browser
knows how to interpretthe binary streams because of the
HTTP protocolresponse header Content-Type
20. Representation Formats
Different clients are able to consume differentrepresentations
of the same resource
A representationcan take various forms, such as:
• image
• a text file
• an XML stream
• a JSON stream
but its resource has to be availablethroughthe same URI
21. Representation Formats
For human-generated requests through a web browser, a
representationis typicallyinthe form of an HTML page
For automatedrequests from other web services, readabilityis
not as importantand a more efficient representationcan be
used such as XML or JSON
23. Actions
Actions are used to operate on resources
For example, they can be usedfor
– getting info about a movie
– addinga photo to Flickr
– deleting a file from a folder
The datatransmitted to and from the resource is a representation
of it
24. HTTP-based Actions
Under HTTP, actions are standardHTTP request:
GET
POST
PUT
DELETE
They make up the uniform interfaceused for client/serverdata
transfers
25. HTTP-based Actions
RESTful web services can also execute logic at the server level,
but remember thatevery result must be a resource
representation
26. HTTP as Uniform Interface
In RESTful systems we focus on resource names, whereas in
traditionalweb systems we focussed on the actions to be
performed on resources
à In RESTful systems we have fourspecific actionsthat we can
take upon resources — Create,Retrieve, Update,and Delete
(CRUD)
In traditionalweb applications,we could have countless actions
with no naming or implementationstandards
27. The Classroom Example
Artificialexample of a web service handling students in some
classroom
Locationof the service = http://restfuljava.com/
Resources are represented as XML streams
28. The Classroom Example: URIs
Student (identified by name):
http://restfuljava.com/students/{name}
List of students:
http://restfuljava.com/students
30. The Classroom Example: Representations
Students List:
<students>
<student>
<name>Jane</name>
<age>10</age>
<link>/students/Jane</link>
</student>
<student>
<name>John</name>
<age>11</age>
<link>/students/John</link>
</student>
</students>
31. GET
The method GET is used to RETRIEVE resources
It cannot have side-effects
àit can be done repeatedly withoutchanging the state of the
resource
It can also returnonly parts of the resource
à it can act as both a read operation and a query operation
33. POST
The method POST is used to CREATE resources
Usually,the resource identity/URLis not known at creationtime
à The URL of the newly createdresource is usually created
automaticallybythe server
35. PUT
The method PUT is used to UPDATE resources
Recurrent PUT workflow:
1. we firstGET the representationof the resource we need to
update
2. in the client we update the resource with the new value(s)
3. we update the resource using a PUT request togetherwith
the representationas its payload
39. A note on PUT and DELETE
PUT and DELETE apply to the entire resource
à when doinga PUT or DELETE operation,
the entire resource is replaced/deleted
The PUT and DELETE operations are atomic
à if two PUT/DELETE operations occur simultaneously,
one of them will win and determine the final state of
the resource
40. HTTP Status Codes
RESTful services use these codes to return informationaboutthe
response of the requests
1xx informationalmessage
2xx success message
3xx redirectsthe client to another URL
4xx client-side error
5xx server-side error
41. LAB
Extend the app of the previous labs so that products and
producers are real data coming from the Loveitaly VM
Another example of Rest API: http://www.nestoria.co.uk/help/api
http://api.nestoria.it/api?place_name=laquila&encoding=json&pretty=1&listing_type=buy&country=it&action=search_listings