REST�RESTful Web Services
Jakub Klímek, Martin Nečaský
This work is licensed under a Creative Commons Attribution 4.0 International License.
What is REST?
2
A truly RESTful API looks like hypertext.
Principle 1: Resource Orientation
3
Most commonly forgotten
Principle 2: Unique Resource Identification
4
Principle 3: Stateless Client/Server Communication
5
Principle 3: Stateless Client/Server Communication
6
Principle 4: Resource Manipulation
http://www.company.org/addCustomer?name=John
7
REST != HTTP
8
REST using HTTP: Resource ID - direct dereferencing
9
https://api.twitter.com/1.1/statuses/user_timeline.json
https://api.twitter.com/1.1/statuses/user_timeline.xml
REST using HTTP: Resource ID - content negotiation
dereferencing using HTTP 303 See Other response code
10
REST using HTTP: Resource ID
11
Server
client
Server
client
HTTP/1.1 303 See Other
Location: http://www.company.org/customer.xml?name=John
Server
client
REST using HTTP: Resource ID
12
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^customer customer.rdf [R=303]
http://www.company.org/
customer?name=John
http://www.company.org/
customer.rdf?name=John
REST using HTTP: Resource ID - Clean URLs
http://www.company.org/customer/John
http://www.company.org/customer?name=John
13
RewriteCond %{HTTP_ACCEPT} application/rdf\+xml
RewriteRule ^customer/([a-zA-Z]+)$ customer.rdf?name=$1
REST using HTTP: HTTP Verbs
when HTTP is used, the following four operations are usually considered:
14
Richardson Maturity Model - RMM
15
Hypermedia
HTTP
URI
Richardson Maturity Model - RMM
16
Richardson Maturity Model - RMM
17
RMM - Level 0 WS
18
RMM - Level 0 WS - example - 1/3
See free slots to see a doctor
19
POST /appointmentService HTTP/1.1
[various other headers]
<openSlotRequest date="2010-01-04" doctor="mjones"/>
HTTP/1.1 200 OK
[various headers]
<openSlotList>
<slot start="1400" end="1450">
<doctor id="mjones"/>
</slot>
<slot start="1600" end="1650">
<doctor id="mjones"/>
</slot>
</openSlotList>
RMM - Level 0 WS - example - 2/3
Book the appointment - success
20
POST /appointmentService HTTP/1.1
[various other headers]
<appointmentRequest>
<slot doctor="mjones" start="1400" end="1450"/>
<patient id="jsmith"/>
</appointmentRequest>
HTTP/1.1 200 OK
[various headers]
<appointment>
<slot doctor="mjones" start="1400" end="1450"/>
<patient id="jsmith"/>
</appointment>
RMM - Level 0 WS - example - 3/3
Book the appointment - failure
21
POST /appointmentService HTTP/1.1
[various other headers]
<appointmentRequest>
<slot doctor="mjones" start="1400" end="1450"/>
<patient id="jsmith"/>
</appointmentRequest>
HTTP/1.1 200 OK
[various headers]
<appointmentRequestFailure>
<slot doctor="mjones" start="1400" end="1450"/>
<patient id="jsmith"/>
<reason>Slot not available</reason>
</appointmentRequestFailure>
RMM - Level 1 WS
22
RMM - Level 1 WS - example - 1/2
See free slots to see a doctor
23
POST /doctors/mjones HTTP/1.1
[various other headers]
<openSlotRequest date = "2010-01-04"/>
HTTP/1.1 200 OK
[various headers]
<openSlotList>
<slot id="1234" doctor="mjones" start="1400" end="1450"/>
<slot id="5678" doctor="mjones" start="1600" end="1650"/>
</openSlotList>
RMM - Level 1 WS - example - 2/2
Book the appointment
24
POST /slots/1234 HTTP/1.1
[various other headers]
<appointmentRequest>
<patient id="jsmith"/>
</appointmentRequest>
HTTP/1.1 200 OK
[various headers]
<appointment>
<slot id="1234" doctor="mjones" start="1400" end="1450"/>
<patient id="jsmith"/>
</appointment>
RMM - Level 2 WS
25
RMM - Level 2 WS - example - 1/3
See free slots to see a doctor
26
GET /doctors/mjones/slots?date=20100104&status=open HTTP/1.1
Host: royalhope.nhs.uk
HTTP/1.1 200 OK
[various headers]
<openSlotList>
<slot id="1234" doctor="mjones" start="1400" end="1450"/>
<slot id="5678" doctor="mjones" start="1600" end="1650"/>
</openSlotList>
RMM - Level 2 WS - example - 2/3
Book the appointment - success
27
POST /slots/1234 HTTP/1.1
[various other headers]
<appointmentRequest>
<patient id="jsmith"/>
</appointmentRequest>
HTTP/1.1 201 Created
Location: slots/1234/appointment
[various headers]
<appointment>
<slot id="1234" doctor="mjones" start="1400" end="1450"/>
<patient id="jsmith"/>
</appointment>
http://royalhope.nhs.uk/slots/1234/appointment
RMM - Level 2 WS - example - 3/3
Book the appointment - failure
28
POST /slots/1234 HTTP/1.1
[various other headers]
<appointmentRequest>
<patient id="jsmith"/>
</appointmentRequest>
HTTP/1.1 409 Conflict
[various headers]
<openSlotList>
<slot id="5678" doctor="mjones" start="1600" end="1650"/>
</openSlotList>
RMM - Level 3 WS
29
Some people pronounce it as "hate-ee-os," similar to "hideous," or as "hate O-A-S".
People also refer to it as a hypermedia-driven system.
RMM - Level 3 WS - example - 1/2
See free slots to see a doctor
30
GET /doctors/mjones/slots?date=20100104&status=open HTTP/1.1
Host: royalhope.nhs.uk
HTTP/1.1 200 OK
[various headers]
<openSlotList>
<slot id="1234" doctor="mjones" start="1400" end="1450">
<link rel= "/linkrels/slot/book"
type="POST"
href="/slots/1234"/>
</slot>
<slot id="5678" doctor="mjones" start="1600" end="1650">
<link rel= "/linkrels/slot/book"
type="POST"
href="/slots/5678"/>
</slot>
</openSlotList>
RMM - Level 3 WS - example - 2/2
Book the appointment
31
POST /slots/1234 HTTP/1.1
[various other headers]
<appointmentRequest>
<patient id="jsmith"/>
</appointmentRequest>
HTTP/1.1 201 Created
Location: http://royalhope.nhs.uk/slots/1234/appointment
[various headers]
<appointment>
<slot id="1234" doctor="mjones" start="1400" end="1450"/>
<patient id="jsmith"/>
<link rel="/linkrels/appointment/cancel"
href="/slots/1234/appointment"/>
<link rel= "/linkrels/appointment/addTest"
href="/slots/1234/appointment/tests"/>
…
</appointment>
REST API
32
RESTful Web Services
33
RESTful Web Services: typical HTTP verb meanings
34
Operation | Resource representing collection of individuals | Resource representing individual |
GET | List members in collection
| Retrieve individual
|
PUT | Update collection with another one
| Update individual
|
DELETE | Delete entire collection
| Delete individual
|
POST | Create member of collection with auto-ID
| Create part of individual
|
REST vs. SOAP - The letter analogy
35
REST vs. SOAP
SOAP and REST – both provide support for building SOA based applications
36
Dimension | REST | SOAP (W3C-style WS) |
Underlying Protocol | REST is an architecture style
| SOAP itself is a protocol
|
Data Format | Any format: JSON, CSV, RSS, XML, … | XML |
Statefulness | Stateless | Stateless |
Caching | Using infrastructure already in place
| |
HTTP Verbs | GET, POST, PUT, DELETE, PATCH | POST |
Security | TLS (HTTPS) (point to point) | WS-Security (end to end) |
Asynchronous Processing | HTTP 202 Accepted response�+ Location of queue | WS-Reliable Messaging |
WADL
37
WADL - example
38
<application xmlns="http://wadl.dev.java.net/2009/02">� <resources base="http://example.com/api">� <resource path="books">� <method name="GET"/>� <resource path="{bookId}">� <param required="true" style="template" name="bookId"/>� <method name="GET"/>� <method name="DELETE"/>� <resource path="reviews">� <method name="GET">� <request>� <param name="page" required="false" default="1" style="query"/>� <param name="size" required="false" default="20" style="query"/>� </request>� <response status="200"> � <representation mediaType="application/xml"/> � <representation mediaType="application/json"/> � </response>� </method>� </resource>� </resource>� </resource>� <resource path="readers">� <method name="GET"/>� </resource>� </resources>�</application>
@base - base URI for child resource identifiers
@path - relative URI template
methods available on the resource
response types
query parameters
WADL - parameter styles
<resource path="reviews">� <method name="GET">� <request>� <param name="page" required="false" default="1" style="query"/>� <param name="size" required="false" default="20" style="query"/>� </request>� </method>�</resource>
39
http://example.com/api/books/1234/reviews?page=1&size=20
{bookID}
http://example.com/api/books/1234/reviews;page=1;size=20