COMP519 Web Programming
Lecture 27: REST
Handouts
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
1
Representational State Transfer (REST)
Motivation
REST Principles
Resources
Operations
2
Further Reading
COMP519 Web Programming Lecture 27 Slide L27 1
Representational State Transfer (REST) Motivation
Web Services: Motivation
There is a lot of data that is of interest to a multitude of parties
Examples:
Addresses for a given postcode
Pictures associated with certain criteria such as location, year, or name
It is in the interest of the owner/collator of that data to make access
easy and automatable, possibly in exchange for payment
Often it is also useful to be able to exchange data in both directions
Examples:
A credit score company that provides credit scores for individuals to banks
and receives information on loans back
A picture database that provides access to pictures but also allows
photographers to upload pictures
It is in the interest of the owner/collator of that data to make the
submission of additional data easy and automatable
COMP519 Web Programming Lecture 27 Slide L27 2
Representational State Transfer (REST) Motivation
Web Services and Representational State Transfer
Web Service
A software system designed to support interoperable machine-to-machine
interaction over the world wide web
Representational State Transfer (REST)
A software architectural style used in the creation of web services expressed
as six constraints on the elements of a software architecture
RESTful Web Service
A web service with a REST architecture
COMP519 Web Programming Lecture 27 Slide L27 3
Representational State Transfer (REST) REST Principles
Representational State Transfer
Client-Server:
A server providing a set of services listens to requests for these services
A client that wants to use a particular service sends a request to the server
The server can then either reject or execute the requested service and
return a response to the client
Stateless:
Communication between client and server must be done without storing
any type of state on the server
; every client request must hold all information necessary for it
to be processed
Cacheability:
Responses must implicitly or explicitly indicate whether they are cacheable or
non-cachable
Clients and intermediaries can cache cachable responses and reuse cached
responses for later equivalent requests
COMP519 Web Programming Lecture 27 Slide L27 4
Representational State Transfer (REST) REST Principles
Representational State Transfer
Layered system
A client is ignorant whether it is connected directly to a server or to an
intermediary
Intermediaries might add a security layer on top of a web service
A server may itself act as a client to multiple other servers to generate a
response to a client
Code on demand:
Servers can temporarily extend or customize the functionality of a client
by transferring executable code, for example, JavaScript, to it
COMP519 Web Programming Lecture 27 Slide L27 5
Representational State Transfer (REST) REST Principles
Representational State Transfer
Uniform interface:
Resource identification in requests
REST treats data / content as resources and collections of resources
Resources are identified using Unique Resource Identifiers (URIs) in a request
The resources themselves are conceptually separate from the representations
that are returned to the client
Resource manipulation through representations
When a client holds a representation of a resource, including any metadata
attached, it has enough information to modify or delete the resource
Self-descriptive requests / responses
Each request and each response includes enough information to describe how
to process it
Hypermedia as the engine of application state (HATEOAS)
Having accessed an initial URI for a RESTful web service, a client should be
able to use server-provided links to dynamically discover all the available
services
COMP519 Web Programming Lecture 27 Slide L27 6
Representational State Transfer (REST) REST Principles
Example
We want to provide a web service that maintains information on
Computer Science students at the University of Liverpool
The web service should allow us to
Retrieve information on all students
Retrieve information on a specific student (via student id)
Add information on a new student
Modify information on an existing student
Delete (information on) a student
COMP519 Web Programming Lecture 27 Slide L27 7
Representational State Transfer (REST) Resources
RESTful Web Services: Resources and URIs
A resource can be a singleton or a collection
A resource may contain sub-collections of resources
; resources form a hierarchical structure
students
is a collection of all students
students/201912345
is a singleton student in that collection
students/201912345/addresses
is a sub-collection of addresses for a student
students/201912345/addresses/tAddr
is a singleton address in that sub-collection
COMP519 Web Programming Lecture 27 Slide L27 8
Representational State Transfer (REST) Resources
RESTful Web Services: Resources and URIs
A resource can be a singleton or a collection
A resource may contain sub-collections of resources
; resources form a hierarchical structure
students/201912345
is a singleton student in the students collection
students/201912345/addresses/tAddr
is a singleton address among all addresses of a student
A REST API uses Uniform Resource Identifiers (URIs) to address
resources
https://api.liv.ac.uk/students/201912345
is the URI for the student with id 201912345
https://api.liv.ac.uk/students/201912345/addresses/tAddr
is the URI for that student’s term time address
provided https://api.liv.ac.uk is the base URL for our web service
COMP519 Web Programming Lecture 27 Slide L27 9
Representational State Transfer (REST) Resources
Naming Conventions for Resources
Include a version number either into the base URL or make it the
top-level of the resource hierarchy
https://v1.api.liv.ac.uk/students/
https://api.liv.ac.uk/v1/students/
Use nouns, not verbs, to represent resources
; use of identification numbers is permissible
students not getStudents
Use singular nouns for singleton resources
tAddr not tAddrs
Use plural nouns for collections of resources
students not student
Use camelCasing for compound nouns
tAddr not term-time nor t_addr
Use forward slash ( / ) to indicate a hierarchical relationship
students/201912345 not students-201912345
COMP519 Web Programming Lecture 27 Slide L27 10
Representational State Transfer (REST) Operations
RESTful Web Services: Operations and HTTP Methods
A REST API associates a specific HTTP method with
each specific operation that can be performed on a resource
The association should reflect the generic meaning of a HTTP method
‘Retrieve information’ is associated with GET
‘Delete information’ is associated with DELETE
A REST API uses HTTP response codes to indicate each specific
outcome of a request
200 (OK) is associated with a successful operation
404 (NOT FOUND) is associated with a failure to find a resource
COMP519 Web Programming Lecture 27 Slide L27 11
Representational State Transfer (REST) Operations
RESTful Web Services: Operations and HTTP Methods
The generic meaning of a HTTP method depends on whether the URI
involved is for
a collection of resources
a singleton / individual resource
As part of a collection, an individual resource is also called member
resource
Properties of requests and responses that are of interest:
Cachable response:
Clients and intermediaries can cache cachable responses and reuse cached
responses for later equivalent requests
Safe request:
A safe request does not change the state of the resource
Idempotent request:
Repeating an idempotent request must produce the same effect on the
server (but not necessarily the same response)
COMP519 Web Programming Lecture 27 Slide L27 12
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
GET on a member resource (cacheable, idempotent, safe)
Retrieve a representation of the resource
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
GET / students /2019 1 23 4 56 / addresses / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 200 OK
Content - Type : applica t io n / json
{" streetH N " :"1 Abby Road ", " city ": " Li v erp o o l ",
" postCode " : " L69 9 AA " , " country ": "UK" }
COMP519 Web Programming Lecture 27 Slide L27 13
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
GET on a member resource (cacheable, idempotent, safe)
Retrieve a representation of the resource
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
GET / students /20501 2 34 5 6 HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 404 NOT FOUND
The HTTP response code 404 in the example above indicates that
a student with that student id does not exist
(assuming we know that the collection students exists)
COMP519 Web Programming Lecture 27 Slide L27 14
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
GET on a member resource (cacheable, idempotent, safe)
Retrieve a representation of the resource
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
GET / students /2019 1 23 0 00 / addresses / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 404 NOT FOUND
Same HTTP response code but fault is not immediately obvious
(student with id 2019123000 may not exist or
the student exists but has no term time address)
COMP519 Web Programming Lecture 27 Slide L27 15
Representational State Transfer (REST) Operations
GET on Complex Member Resources
If a member resource M ‘contains’ a collection or other member resource,
we can represent M as follows:
via embedding: A full representation of the collections and member
resources is embedded in the representation of M
{" id ":" 2 0 1 91 2 34 5 6 " , " sname ": " Ady " , " fname ": "Ada ",
" addresse s " :
[" tAddr ":{ " streetHN " : "1 Abby Road " , " city ":" L i ver p o ol " ,
" postCode " : " L69 9AA ", " country ":" UK "},
" pAddr " :{ " streetHN ": "9 Mort Street " , " city " :" Wigan " ,
" postCode " : " WN2 4TU ", " country ":" UK " }]}
This becomes problematic if either the collections are very large or we
are trying to model a circular relationship, for example, if we try to add
an attribute "tutor" for a member resource that in turn contains a
collection "tutees"
COMP519 Web Programming Lecture 27 Slide L27 16
Representational State Transfer (REST) Operations
GET on Complex Member Resources
If a member resource M ‘contains’ a collection or other member resource,
we can represent M as follows:
via links: Links to other resources are embedded
{" id ":" 2 0 1 91 2 34 5 6 " , " sname ": " Ady " , " fname ": "Ada ",
" _links " :[{ " href ":" stu d e n ts /201 9 12 34 5 6/ addr e s se s " ,
" method ":" GET " ," rel ": " addresses "} ,
{" href " :" students / 2 01 9 12 3 45 6 ",
" method ":" GET " ," rel ": " self " }]}
This provides the most ‘compact’ representation
via relationships: Links to each member resource of each collection are
embedded
{" id ":" 2 0 1 91 2 34 5 6 " , " sname ": " Ady " , " fname ": "Ada ",
" rela ti on sh i ps ": {
" addresse s " :[ " students /2019 12 3 45 6/ addre s s es / tAddr " ,
" students /2019 1 23 4 56 / addresses / pAddr " ]}}
This is most appropriate if those member resources exist independently,
for example, modules, academic advisors (but not addresses)
COMP519 Web Programming Lecture 27 Slide L27 17
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
GET on a member resource (cacheable, idempotent, safe)
Retrieve a representation of the resource
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
GET / students /20191 2 34 5 6 HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 200 OK
Content - Type : applica t io n / json
{" id ":" 2 0 1 91 2 34 5 6 " , " sname ": " Ady " , " fname ": "Ada ",
" _links " :[{ " href ":" stu d e n ts /201 9 12 34 5 6/ addr e s se s " ,
" method ":" GET " ," rel ": " addresses "} ,
{" href " :" students / 2 01 9 12 3 45 6 ",
" method ":" GET " ," rel ": " self " }]}
COMP519 Web Programming Lecture 27 Slide L27 18
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
DELETE on a member resource ( cacheable , idempotent, not safe)
Delete that member resource
Response codes: 200 (OK) , 204 (NO CONTENT) , 404 (NOT FOUND)
Request :
DELETE / students /2 0 19 1 23 45 6 HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 204 NO CONTENT
HTTP response code 204 indicates that deletion was successful and
that there is no response body
HTTP response code 200 indicates that deletion was successful but
we also send data back, e.g., a representation of what was deleted
HTTP response code 404 can be used if the member resource
did not exist
COMP519 Web Programming Lecture 27 Slide L27 19
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
POST on a member resource (not cacheable, not idempotent, not safe)
Create a new specific member resource
using the data in the request body
The URI of the member resource is returned in
the response Location header field
R’Cs: 201 (CREATED) , 400 (BAD REQUEST) , 409 (CONFLICT)
Request :
POST / students /201 9 12 34 58 / add r e sse s / tAddr HTTP /1.1
Host : v 1. api . liv . ac .uk
Content - Type : applica t io n / json ; charset =utf -8
{" streetH N " :"3 Carl ' s Court " , " city ":" Liv e r poo l " ,
" postCode " : "L 69 3 CC " ," country " :" UK "}}
Response :
HTTP /1.1 201 CREATED
Location : https :// v 1. api .liv .ac . uk / st u dents /20 1 91 23 4 58 /
, addresses / tAddr
COMP519 Web Programming Lecture 27 Slide L27 20
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
POST on a member resource (not cacheable, not idempotent, not safe)
Create a new specific member resource
using the data in the request body
The URI of the member resource is returned in
the response Location header field
R’Cs: 201 (CREATED) , 400 (BAD REQUEST) , 409 (CONFLICT)
Request :
POST / students /201 9 12 34 58 / add r e sse s / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Content - Type : applica t io n / json ; charset =utf -8
{" streetH N " :"1 Abby Road ", " city ": " Li v erp o o l "}
Response :
HTTP /1.1 400 BAD REQUEST
HTTP response code 400 indicates that not enough or incorrect data was
provided for the new member resource, e.g., no post code was provided
COMP519 Web Programming Lecture 27 Slide L27 21
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
POST on a member resource (not cacheable, not idempotent, not safe)
Create a new specific member resource
using the data in the request body
The URI of the member resource is returned in
the response Location header field
R’Cs: 201 (CREATED) , 400 (BAD REQUEST) , 409 (CONFLICT)
Request :
POST / students /201 9 12 34 57 / add r e sse s / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Content - Type : applica t io n / json ; charset =utf -8
{" streetH N " :"1 Abby Rd " ," city " :" Lvpl " ," p o s t Code " :" L69 9 AA "}
Response :
HTTP /1.1 409 CONFL I C T
HTTP response code 409 indicates that the new member resource
conflicts with an already existing one, e.g., the student already had a
term time address
COMP519 Web Programming Lecture 27 Slide L27 22
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
PUT on a member resource (not cacheable, idempotent, not safe)
If the member resource exists,
replace it with one based on the information in the request body
If the member resource does not exist,
behave like POST on a member resource
R’Cs: 200 (OK) + response codes for POST
Request :
PUT / students /2019 1 23 4 56 / addresses / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Content - Type : applica t io n / json ; charset =utf -8
{" streetH N " :"2 Bank Lane ", " city ": " Li v erp o o l ",
" postCode " : " L69 2 BB " , " country ": "UK" }
Response :
HTTP /1.1 200 OK
HTTP response code 200 indicates that the member resource was suc-
cessfully replaced and we send the modified member resource back
COMP519 Web Programming Lecture 27 Slide L27 23
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
PUT on a member resource (not cacheable, idempotent, not safe)
If the member resource exists,
replace it with one based on the information in the request body
If the member resource does not exist,
behave like POST on a member resource
R’Cs: 200 (OK) + response codes for POST
Request :
PUT / students /2019 1 23 4 56 / addresses / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Content - Type : applica t io n / json ; charset =utf -8
{" streetH N " :"2 Bank Lane ", " city ": " Li v erp o o l "}
Response :
HTTP /1.1 400 BAD REQUEST
HTTP response code 400 indicates that not enough or incorrect data
was provided for the update, e.g., no post code was provided
COMP519 Web Programming Lecture 27 Slide L27 24
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
PATCH on a member resource (not cacheable, idempotent, not safe)
If the member resource exists,
update parts of the member resource using the data in the
request body
If the member resource does not exist,
report an error or create the member resource using the data
in the request body
Response codes: 200 (OK) , 404 (NOT FOUND)
COMP519 Web Programming Lecture 27 Slide L27 25
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
PATCH on a member resource (not cacheable, idempotent, not safe)
If the member resource exists,
update parts of the member resource using the data in the
request body
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
PATCH / stu d e n ts /201 9 12 34 5 6/ addr e s se s / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Content - Type : applica t io n / json ; charset =utf -8
{" streetH N " :"2 Bank Lane ", " postCode " : " L69 2 BB "}
Response :
HTTP /1.1 200 OK
Content - Type : applica t io n / json
{" streetH N " :"2 Bank Lane ", " city ": " Li v erp o o l ",
" postCode " : " L69 2 BB " , " country ": "UK" }
COMP519 Web Programming Lecture 27 Slide L27 26
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
PATCH on a member resource (not cacheable, idempotent, not safe)
If the member resource exists,
update parts of the member resource using the data in the
request body
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
PATCH / stu d e n ts /201 9 12 34 5 6/ addr e s se s / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Content - Type : applica t io n / json ; charset =utf -8
{" streetH N " :"2 Bank Lane ", " postCode " : " L69 2 BB "}
Response :
HTTP /1.1 200 OK
...
HTTP response code 200 indicates that the member resource was suc-
cessfully updated with data in the request body
Note: Incomplete data is fine
COMP519 Web Programming Lecture 27 Slide L27 27
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
PATCH on a member resource (not cacheable, idempotent, not safe)
If the member resource does not exist,
report an error or create the member resource using the data
in the request body
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
PATCH / stu d e n ts /201 9 12 34 5 6/ addr e s se s / pAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Content - Type : applica t io n / json ; charset =utf -8
{" streetH N " :"2 Bank Lane ", " postCode " : " L69 2 BB "}
Response :
HTTP /1.1 404 NOT FOUND
HTTP response code 404 indicates that the member resource did not
exist and therefore could not be updated
COMP519 Web Programming Lecture 27 Slide L27 28
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
POST on a collection (not cacheable, not idempotent, not safe)
Create a new member resource in the collection using the information
in the request body
The URI of the created member resource is automatically assigned
and returned in the response Location header field
Response codes: 201 (CREATED) , 400 (BAD REQUEST)
Request :
POST / students HTTP /1.1
Host : v 1. api . liv . ac .uk
Content - Type : applica t io n / json ; charset =utf -8
{" sname ":" Clay " ," fname ": "Cia ",
" tAddr " :{ " streetHN ": "3 Carl ' s Court ", " city ": " Liverpool " ,
" postCode " : "L 69 3 CC " ," country " :" UK "}}
Response :
HTTP /1.1 201 CREATED
Location : https :// v 1. api .liv .ac . uk / st u dents /201 9 12 34 5 8
COMP519 Web Programming Lecture 27 Slide L27 29
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
POST on a collection (not cacheable, not idempotent, not safe)
Create a new member resource in the collection using the information
Response codes: 201 (CREATED) , 400 (BAD REQUEST)
Request :
POST / students HTTP /1.1
Host : v1 . api . liv .ac . uk
Content - Type : applica t io n / json ; charset =utf -8
{" sname ":" Deng " ,
" tAddr " :{ " streetHN ": "3 Carl ' s Court " , " city ":" Liv e r poo l " ,
" postCode " : " L69 3 CC " ," country " :" UK "}}
Response :
HTTP /1.1 400 BAD REQUEST
HTTP response code 400 indicates that not enough or incorrect data
was provided, e.g., no first name was provided
How much data is ‘enough’ will depend on the web service
COMP519 Web Programming Lecture 27 Slide L27 30
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
DELETE on a collection (not cacheable, idempotent, not safe)
Delete all member resources of the collection but not the collection
Response codes: 200 (OK) , 204 (NO CONTENT) , 404 (NOT FOUND)
Request :
DELETE / students HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 204 NO CONTENT
HTTP response code 204 indicates that deletion was successful and
that there is no response body
HTTP response code 200 indicates that deletion was successful but
we also send data back, e.g., a representation of what was deleted
HTTP response code 404 can be used if the collection
did not exist
COMP519 Web Programming Lecture 27 Slide L27 31
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
GET on a collection (cacheable, idempotent, safe)
Retrieve the URIs or representations of all the member resources
of the collection
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
GET / students HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 200 OK
Content - Type : applica t io n / json
[{ " id ":" 201 9 12 3 45 6 " , " sname ":" Ady " , "fname ": " Ada " ,
" tAddr " :{ " streetHN ": "1 Abby Road " , " city " : " Liverpool " ,
" postCode " : " L69 9 AA "}} ,
{" id ":" 2 0 1 91 2 34 5 7 " , " sname ": " Bain ", " fname " :" Ben ",
" tAddr " :{ " streetHN ": "2 Bank Lane " , " city " : " Liverpool " ,
" postCode " : " L69 2 BB " }}]
COMP519 Web Programming Lecture 27 Slide L27 32
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
GET on a collection (cacheable, idempotent, safe)
Retrieve the URIs or representations of all the member resources
of the collection
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
GET / students HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 200 OK
Content - Type : applica t io n / json
[{ " id ":" 201 9 12 3 45 6 " ," sname ":" Ady " ," fname ":" Ada " ," _links " :
[{ " href ": " students / 20 19 12 3 45 6/ addr e s ses " ," method ":" GET " ,
" rel ":" ad d r ess e s " } ,{ " href ":" s t u d ents /201 9 12 3 45 6 ",
" method ":" GET " ," rel ": " self " }]} ,
{" id ":" 2 0 1 91 2 34 5 7 " ," sname ":" Bain "," fname " :" Ben "," _links ":
[{ " href ": " students / 20 19 12 3 45 7/ addr e s ses " ," method ":" GET " ,
" rel ":" ad d r ess e s "} , ...]}]
COMP519 Web Programming Lecture 27 Slide L27 33
Representational State Transfer (REST) Operations
RESTful Web Services and HTTP Methods
GET on a collection (cacheable, idempotent, safe)
Retrieve a representation of the resource
Response codes: 200 (OK) , 404 (NOT FOUND)
Request :
GET / academic s HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 404 NOT FOUND
The HTTP response code 404 in the example above indicates that
the collection academics does not exist
COMP519 Web Programming Lecture 27 Slide L27 34
Representational State Transfer (REST) Operations
Query Parameters: Filtering / Sorting
RESTful Web Services often allow the response to a GET request on a
collection to be modified by query parameters
GET /students?sort=sname
returns the list of all students ordered by their surname
GET /students?sname_contains=Smith
returns the list of all students whose surname contains the
substring Smith
GET /students?sname_contains=Smith&sort=id
returns the list of all students whose surname contains the substring
Smith ordered by student ID
COMP519 Web Programming Lecture 27 Slide L27 35
Representational State Transfer (REST) Operations
Query Parameters: Pagination
Pagination can be used to return a representation of only a part of a
collection in response to a GET request
Request :
GET / students ? page =2 HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 200 OK
Content - Type : applica t io n / json
{ " page _ n um be r ": 2 ,
" total _ pa g es ": 154 ,
" total _ co u nt ": 15342 ,
" prev io us _p ag e ": " / st u dents ? page =1 " ,
" next_pag e " : "/ students ? page =3 " ,
" last_pag e " : "/ students ? page =154 ",
" results ": [ ... ] }
COMP519 Web Programming Lecture 27 Slide L27 36
Representational State Transfer (REST) Operations
HATEOAS Revisited
HATEOS
Having accessed an initial URI for a RESTful web service, a client should
be able to use server-provided links to dynamically discover all the available
services
Following this principle, data returned in response to requests should also
include information about the actions that can be performed
COMP519 Web Programming Lecture 27 Slide L27 37
Representational State Transfer (REST) Operations
HATEOAS Revisited
Accessing the ‘root’ of a web service should give us a list of links and
HTTP request methods that we can use with this service
Request :
GET / HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 200 OK
Content - Type : applica t io n / json
{ " _links ": [ { " href ": " / st u dents " , " method " : "GET ",
" rel ": " collec t io n " },
{ " href ": " / st u d e n ts " , " method " : " POST ",
" rel ": " edit " },
{ " href ": " / st u d e n ts " , " method " : " DELETE " ,
" rel ": " delete " }
] }
COMP519 Web Programming Lecture 27 Slide L27 38
Representational State Transfer (REST) Operations
HATEOAS Revisited
Accessing a collection (member resource) should give us a list of links and
HTTP request methods that we can use on that collection (member
resource)
Request :
GET / students /2019 1 23 4 56 / addresses / tAddr HTTP /1.1
Host : v1 . api . liv .ac . uk
Response :
HTTP /1.1 200 OK
Content - Type : applica t io n / json
{" street H N " :"1 Abby Road ", " city ": " Li v erp o o l ",
" postCode " : " L69 9 AA " , " country ": "UK" ,
" _links ": [{ " href ":" / stu d e n ts /201 9 12 34 56 / addr e sse s / tAddr " ,
" method ": " GET ", " rel ": " self " },
{" href " :"/ students /2019 1 23 45 6/ addre s s es / tAddr " ,
" method ": " PATCH " , "rel ": " edit " },
{" href " :"/ students /2019 1 23 45 6/ addre s s es / tAddr " ,
" method ": " DELETE ", " rel ": " delete " } ] }
COMP519 Web Programming Lecture 27 Slide L27 39
Further Reading
Revision and Further Reading
Read
Chapter 1: Introduction to REST
Chapter 4: Resource-Oriented Services: Designing Services
of S. Abeysinghe: RESTful PHP Web Services.
Packt Publishing, 2008.
COMP519 Web Programming Lecture 27 Slide L27 40