Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

CSE 2034Y

Internet Technologies and Web


Services

Lecture

RESTful Web Services – Part 1 Version: 1.0-15

Mr Shehzad Jaunbuccus
University of Mauritius

Email: s.jaunbuccus@uom.ac.mu
Overview

» Definition of REST.

» Resources & Collections

» What makes a service RESTful?

» RESTful URIs

» Data trasmission in RESTful web services

» Data Representation.

» Operation on resources.

2
REST (REpresentational State Transfer)

What is REST?
REST is a set of principles which guide the transfer of data in
a meaningful, elegant and efficient manner.

You may consider REST as an design style. Application which


endorse this style are typically referred to as RESTful
applications.
Similarly, web services which support this style are termed
as RESTful web services.

REST is not tied to the HTTP protocol, but it uses that


protocol in a natural way:
Uses http verbs (POST, GET, etc) to operate on resources
3
Resources and collections

Resources can be anything: files, a single row from a


database table, a post on a forum, etc.
A resource has to have a URI.

A Collection is a group of resources of the same


type.

Q1: List possible resources and collections for a


library. Provide justification for each of the items
you have identified above.
4
What makes a service RESTful?

A RESTful service uses:


• The HTTP verb (HTTP method) as the
operation to carry
• Adds scoping information (the data to be
operated on) in the URI
• Every resource (piece of data which can be
operated on) has a unique URI.

5
RESTful URIs
RESTful services use very descriptive URIs (URLs)
Users can have an idea about the data to operate on just by looking at
the URL of a RESTful service.
E.g. The URL to request details concerning the experience of lecturers
for a particular module (CSE1023Y) might look like the URL below:
http://www.shehzad.edu/LecturerExperience/CSE1023Y

The SOAP equivalent might look like:


<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:we="http://www.shehzad.edu/LecturerExperience">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<we:ModuleCode>CSE1023Y</we:ModuleCode>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
6
RESTful URIs (when performing searches)
More complex request which require more than
one parameter may be sent as key/value pairs in a
query string.
E.g., Experience of lecturers for ‘web’ related
modules for the ‘CS’ (Computer Science)
programme.
Write the possible URL for the above example.

7
SOAP Web Service (Data transmission)

Image downloaded from: http://www.service-architecture.com/articles/web-services/web_services_explained.html 8


RESTful Web Service (Data transmission)
Notice that there is no request body

Image downloaded from: http://www.service-architecture.com/articles/web-services/web_services_explained.html 9


Data representation (think response body)

Within a RESTful architecture, data is usually


transferred in either JSON (JavaScript Object
Notation) or XML format.
XML JSON
<Lecturers> {
<Lecturer> "Lecturers": {
<Name>Shehzad Jaunbuccus</Name> "Lecturer": [
<Module>Web Centric</Module> {
</Lecturer> "Name": "Shehzad Jaunbuccus",
<Lecturer> "Module": "Web Centric"
<Name>Zahra Mungloo- },
Dilmohamud</Name> {
<Module>Object-Oriented "Name": "Zahra Mungloo-Dilmohamud",
Programming</Module> "Module": "Object-Oriented Programming"
</Lecturer> }
</Lecturers> ]
}
}
10
Exercise 1: Convert XML to JSON

Given the following XML, write the JSON


equivalent
<Result>
<LecturerExperience>
<LecturerName>Shehzad Jaunbuccus</LecturerName>
<Module>CSE2041-Web Technology II</Module>
<Experience>2</Experience>
</LecturerExperience>
<LecturerExperience>
<LecturerName>Anwar Chutoo</LecturerName>
<Module>CSE1041-Web Technology I</Module>
<Experience>10</Experience>
</LecturerExperience>
</Result>

11
RESTful Web Service (Data transmission thru’ JSON)

Image downloaded from: http://www.service-architecture.com/articles/web-services/web_services_explained.html 13


Operation on resources: Create, Read, Update, Delete

Manipulation of resources is done through the


HTTP methods (verbs) sent through the request
HTTP Method Operation Purpose and Example URI

GET Read Returns a resource or a collection


http://www.shehzad.edu/modules
Returns a collection of modules
http://www.shehzad.edu/modules/CSE1023Y
Returns details for module with code CSE1023Y
PUT * Create Creates a resource
http://www.shehzad.edu/modules/CSE2034Y
Creates a module with code CSE2034Y
POST * Update Updates a resource
http://www.shehzad.edu/modules/CSE1023Y
Updates the module with code CSE1023Y
DELETE Delete Deletes a resource
http://www.shehzad.edu/modules/CSE1023Y
Deletes the module with code CSE1023Y

14
References

[1] Leonard Richardson and Sam Ruby, RESTful


Web Services, O’Reilly Media, Inc., 2007.
[2] Web Services Explained, Barry & Associates,
Inc., http://www.service-
architecture.com/articles/web-
services/web_services_explained.html
[Accessed March 2015]
[3] Lorna Jane Mitchell, PHP Web Services,
O’Reilly Media, Inc., 2013.

15

You might also like