a n i n t r o d u c t i o n t o
RESTFUL WEB SERVICESFelipe Dornelas
AGENDA
2
▫︎The Internet
▫︎The Web and its Resources
▫︎HTTP
▫︎The Resource-Oriented Architecture
▫︎RESTful Web Services
WHAT IS REST?
3
HTTP + Resource-Oriented Architecture
THE INTERNETA network of networks
4
5
6
THE INTERNET, 2010
7
INTERNET ROUTES
8
INTERNET ROUTES
9
CACHING
10
INTERNET LAYERS
11
Web, E-mail, BitTorrent, DNS…
TCP, UDP…
Internet Protocol (IP)
WiFi, Ethernet, 3G, LTE…
INTERNET LAYERS
12
We will talk about the Web
THE WEBAn application of the Internet
13
WHAT IS THE WEB?
14
An information system of interlinked hypertext documents and resources
accessed via the Internet
HYPERTEXT DOCUMENTS
15
HYPERTEXT MARKUP LANGUAGE
16
<!doctype html><html><head> <title>Example Hypertext Document</title></head><body><div> <h1>Example Hypertext Document</h1> <p>This is an example hypertext document to be used for illustrative purposes.</p> <p><a href=“http://example.org”> Example Hyperlink</a></p></div></body></html>
HYPERTEXT TRANSFER PROTOCOL
18
ServerClient
HTTP Requestexample.comMozilla Firefox
HTTP REQUEST
19
GET / HTTP/1.1User-Agent: Mozilla FirefoxHost: example.comAccept: */*
HYPERTEXT TRANSFER PROTOCOL
20
ServerClient
HTTP Responseexample.comMozilla Firefox
HTTP RESPONSE
21
HTTP/1.1 200 OKContent-Type: text/htmlContent-Length: 1270
<!doctype html><html><head> <title>Example Domain</title></head><body> … </body></html>
22
INTERNET LAYERS
23
HTTP
TCP
Internet Protocol (IP)
WiFi, Ethernet, 3G, LTE…
RESOURCES
24
Anything that can be identified, named, addressed or handled on the Web
RESOURCES
25
▫︎Can be concrete things:
▫︎Web pages
▫︎Files
▫︎Videos
▫︎Blog posts
▫︎Articles
RESOURCES
26
▫︎Can also represent abstract concepts:
▫︎Employees in a enterprise
▫︎Money transfers
▫︎Products in a online store
▫︎Calendar appointments
▫︎User accounts
RESOURCE NAMES
27
▫︎URN - Uniform Resource Name
▫︎products/54321
▫︎about-us
▫︎articles/web.html
▫︎posts/2015-04-13
▫︎podcasts/rest.mp3
RESOURCE LOCATORS
28
▫︎URL - Uniform Resource Locator
▫︎http://example.com/products/54321
▫︎http://example.com/about-us
▫︎http://example.com/articles/web.html
▫︎http://example.com/posts/2015-04-13
▫︎http://example.com/podcasts/rest.mp3
ANATOMY OF AN URL
29
RESOURCE IDENTIFIERS
30
RESOURCE IDENTIFIERS
31
A resource only exists on the Web if it has an identifier (URI)
RESOURCES
32
HTTP can manipulate not only hypertext documents but any type of resources
READING A TEXT RESOURCE
34
http://example.com/hello-world.txt
READING A TEXT RESOURCE
35
GET /hello-world.txt HTTP/1.1Host: example.com
HTTP Request
READING A TEXT RESOURCE
36
HTTP/1.1 200 OKContent-Type: text/plainContent-Length: 13
Hello, World!
HTTP Response
CREATING A TEXT RESOURCE
37
POST / HTTP/1.1Host: example.comContent-Type: text/plain
Hello, Mars!
HTTP Request
CREATING A TEXT RESOURCE
38
HTTP/1.1 201 CreatedLocation: /hello-mars.txt
HTTP Response
CREATING A TEXT RESOURCE
39
http://example.com/hello-mars.txt
RESOURCE DOES NOT EXIST
40
http://example.com/hello-pluto.txt
RESOURCE DOES NOT EXIST
41
GET /hello-pluto.txt HTTP/1.1Host: example.com
HTTP Request
RESOURCE DOES NOT EXIST
42
HTTP/1.1 404 Not Found
HTTP Response
HTTP CONTENT TYPES
43
▫︎Determine the type of the HTTP payload
▫︎text/html - HTML
▫︎text/plain - Plain Text
▫︎audio/mpeg3 - MP3 files
▫︎application/xml - XML
▫︎…
HTTP VERBS
44
▫︎GET
▫︎POST
▫︎PUT
▫︎DELETE
▫︎HEAD
▫︎OPTIONS
HTTP STATUS CODES
45
▫︎Success (2xx)
▫︎200 OK
▫︎201 Created
▫︎204 No Content
▫︎…
HTTP STATUS CODES
46
▫︎Client Error (4xx)
▫︎400 Bad Request
▫︎404 Not Found
▫︎409 Conflict
▫︎…
HTTP STATUS CODES
47
▫︎Server Error (5xx)
▫︎500 Internal Server Error
▫︎503 Server Unavailable
▫︎…
THE RESOURCE-ORIENTED ARCHITECTURE
48
REST
49
Representational State Transfer
REST
50
HTTP + Resource-Oriented Architecture
REST
51
HTTP + Resource-Oriented Architecture
RESTful
EMPLOYEE RESOURCE
52
EMPLOYEE RESOURCE
53
▫︎Alice
▫︎Developer
▫︎Female
▫︎…
XML REPRESENTATION
54
<employee> <name>Alice</name> <role>Developer</role> <gender>female</gender></employee>
JSON REPRESENTATION
55
{ "name": "Alice", "role": "Developer", "gender": "female"}
HTML REPRESENTATION
56
<h1>Alice</h1><dl><dt>Role:</dt> <dd>Developer</dd><dt>Gender:</dt> <dd>Female</dd></dl>
EMPLOYEE RESOURCE
57
/employees
EMPLOYEE RESOURCE
58
/employees/alice
/employees/bob
/employees/eve
RESOURCE OPERATIONS
59
▫︎Create
▫︎Read
▫︎Update
▫︎Delete
▫︎List
LIST EMPLOYEE RESOURCES
60
GET /employees HTTP/1.1Host: example.comAccept: application/xml
HTTP Request
LIST EMPLOYEE RESOURCES
61
HTTP/1.1 200 OKContent-Type: application/xml
<employees> <employee href="/employees/alice"/> <employee href="/employees/bob"/> <employee href="/employees/eve"/></employee>
HTTP Response
READ EMPLOYEE RESOURCE
62
GET /employees/alice HTTP/1.1Host: example.comAccept: application/xml
HTTP Request
READ EMPLOYEE RESOURCE
63
HTTP/1.1 200 OKContent-Type: application/xml
<employee> <name>Alice</name> <role>Developer</role> <gender>female</gender></employee>
HTTP Response
CREATE EMPLOYEE RESOURCE
64
POST /employees HTTP/1.1Host: example.comContent-Type: application/xml
<employee><name>John</name><role>QA</role><gender>male</gender>
</employee>
HTTP Request
CREATE EMPLOYEE RESOURCE
65
HTTP/1.1 201 CreatedLocation: /employees/john
HTTP Response
UPDATE EMPLOYEE RESOURCE
66
PUT /employees/alice HTTP/1.1Host: example.comContent-Type: application/xml
<employee><name>Alice</name><role>Manager</role><gender>female</gender>
</employee>
HTTP Request
UPDATE EMPLOYEE RESOURCE
67
HTTP/1.1 200 OK
HTTP Response
DELETE EMPLOYEE RESOURCE
68
DELETE /employees/alice HTTP/1.1Host: example.com
HTTP Request
DELETE EMPLOYEE RESOURCE
69
HTTP/1.1 204 No Content
HTTP Response
RESOURCE-ORIENTED ARCHITECTURE
70
1. Addressability
2. Statelessness
3. Connectedness
4. Uniform Interface
ADDRESSABILITY
71
Every interesting piece of information the server can provide should be exposed as a resource,
and given its own URI
ADDRESSABILITY
72
http://example.com/employees/alice
STATELESSNESS
73
Every HTTP request should happen in complete isolation
STATELESSNESS
74
http://google.com/search?q=jellyfish
STATELESSNESS
75
STATELESSNESS
76
STATELESSNESS
77
http://google.com/search?q=jellyfish&start=10
STATELESSNESS
78
Application State vs. Resource State
CONNECTEDNESS
79
Documents should contain not just data, but links to other resources
CONNECTEDNESS
80
CONNECTEDNESS
81
CONNECTEDNESS
82
CONNECTEDNESS
83
{ "employees": [ "/employees/alice", "/employees/bob", "/employees/eve", ... ]
"next_page": "/employees?start=10", "create_employee": "/employees"}
HATEOAS
84
Hypermedia As The Engine of Application State
UNIFORM INTERFACE
85
▫︎Create: POST /employees
▫︎Read: GET /employees/alice
▫︎Update: PUT /employees/alice
▫︎Delete: DELETE /employees/alice
▫︎List: GET /employees
UNIFORM INTERFACE
86
▫︎Create: POST /resource
▫︎Read: GET /resource/{name}
▫︎Update: PUT /resource/{name}
▫︎Delete: DELETE /resource/{name}
▫︎List: GET /resource
SAFETY
87
GET and HEAD never change the resource state
INDEMPOTENCE
88
PUT and DELETE are indempotent
RESTFUL WEB SERVICES
89
WEB SERVICES
90
client server
Web
BIG WEB SERVICES
91
▫︎Heavy
▫︎Don’t scale
▫︎Hard to understand
▫︎Tight coupling
▫︎SOAP, WSDL, etc…
TIGHT COUPLING
92
BROKEN TIGHT COUPLING
93
RESTFUL WEB SERVICES
94
▫︎Lightweight
▫︎Cacheable
▫︎Scalable
▫︎Discoverable
▫︎Loose coupling
RESOURCE-ORIENTED ARCHITECTURE
95
1. Addressability
2. Statelessness
3. Connectedness
4. Uniform Interface
CACHEABILITY
96
GET http://example.com/employees/alice
CACHEABILITY
97
GET http://example.com/employees/alice
SCALABILITY
98
GET http://example.com/employees/alice
clientserver
SCALABILITY
99
GET http://example.com/employees/alice
client
server cluster
DISCOVERABILITY
100
DISCOVERABILITY
101
{ "employees": [ "/employees/alice", "/employees/bob", "/employees/eve", ... ]
"next_page": "/employees?start=10", "create_employee": "/employees"}
PUBLIC RESTFUL APIS
102
▫︎GitHub
▫︎Amazon S3
REFERENCE
103
RESTful Web Services Leonard Richardson Sam Ruby
Top Related