Slide 1

Slide 1 text

a n i n t r o d u c t i o n t o RESTFUL WEB SERVICES Felipe Dornelas

Slide 2

Slide 2 text

AGENDA 2 ▫︎The Internet ▫︎The Web and its Resources ▫︎HTTP ▫︎The Resource-Oriented Architecture ▫︎RESTful Web Services

Slide 3

Slide 3 text

WHAT IS REST? 3 HTTP + Resource-Oriented Architecture

Slide 4

Slide 4 text

THE INTERNET A network of networks 4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

THE INTERNET, 2010 7

Slide 8

Slide 8 text

INTERNET ROUTES 8

Slide 9

Slide 9 text

INTERNET ROUTES 9

Slide 10

Slide 10 text

CACHING 10

Slide 11

Slide 11 text

INTERNET LAYERS 11 Web, E-mail, BitTorrent, DNS… TCP, UDP… Internet Protocol (IP) WiFi, Ethernet, 3G, LTE…

Slide 12

Slide 12 text

INTERNET LAYERS 12 We will talk about the Web

Slide 13

Slide 13 text

THE WEB An application of the Internet 13

Slide 14

Slide 14 text

WHAT IS THE WEB? 14 An information system of interlinked hypertext documents and resources accessed via the Internet

Slide 15

Slide 15 text

HYPERTEXT DOCUMENTS 15

Slide 16

Slide 16 text

HYPERTEXT MARKUP LANGUAGE 16 Example Hypertext Document

Example Hypertext Document

This is an example hypertext document to be used for illustrative purposes.

Example Hyperlink

Slide 17

Slide 17 text

HYPERTEXT TRANSFER PROTOCOL 17 Server Client example.com Mozilla Firefox

Slide 18

Slide 18 text

HYPERTEXT TRANSFER PROTOCOL 18 Server Client HTTP Request example.com Mozilla Firefox

Slide 19

Slide 19 text

HTTP REQUEST 19 GET / HTTP/1.1 User-Agent: Mozilla Firefox Host: example.com Accept: */*

Slide 20

Slide 20 text

HYPERTEXT TRANSFER PROTOCOL 20 Server Client HTTP Response example.com Mozilla Firefox

Slide 21

Slide 21 text

HTTP RESPONSE 21 HTTP/1.1 200 OK Content-Type: text/html Content-Length: 1270 Example Domain …

Slide 22

Slide 22 text

22

Slide 23

Slide 23 text

INTERNET LAYERS 23 HTTP TCP Internet Protocol (IP) WiFi, Ethernet, 3G, LTE…

Slide 24

Slide 24 text

RESOURCES 24 Anything that can be identified, named, addressed or handled on the Web

Slide 25

Slide 25 text

RESOURCES 25 ▫︎Can be concrete things: ▫︎Web pages ▫︎Files ▫︎Videos ▫︎Blog posts ▫︎Articles

Slide 26

Slide 26 text

RESOURCES 26 ▫︎Can also represent abstract concepts: ▫︎Employees in a enterprise ▫︎Money transfers ▫︎Products in a online store ▫︎Calendar appointments ▫︎User accounts

Slide 27

Slide 27 text

RESOURCE NAMES 27 ▫︎URN - Uniform Resource Name ▫︎products/54321 ▫︎about-us ▫︎articles/web.html ▫︎posts/2015-04-13 ▫︎podcasts/rest.mp3

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

ANATOMY OF AN URL 29

Slide 30

Slide 30 text

RESOURCE IDENTIFIERS 30

Slide 31

Slide 31 text

RESOURCE IDENTIFIERS 31 A resource only exists on the Web if it has an identifier (URI)

Slide 32

Slide 32 text

RESOURCES 32 HTTP can manipulate not only hypertext documents but any type of resources

Slide 33

Slide 33 text

Imaginary HTTP server: example.com 33

Slide 34

Slide 34 text

READING A TEXT RESOURCE 34 http://example.com/hello-world.txt

Slide 35

Slide 35 text

READING A TEXT RESOURCE 35 GET /hello-world.txt HTTP/1.1 Host: example.com HTTP Request

Slide 36

Slide 36 text

READING A TEXT RESOURCE 36 HTTP/1.1 200 OK Content-Type: text/plain Content-Length: 13 Hello, World! HTTP Response

Slide 37

Slide 37 text

CREATING A TEXT RESOURCE 37 POST / HTTP/1.1 Host: example.com Content-Type: text/plain Hello, Mars! HTTP Request

Slide 38

Slide 38 text

CREATING A TEXT RESOURCE 38 HTTP/1.1 201 Created Location: /hello-mars.txt HTTP Response

Slide 39

Slide 39 text

CREATING A TEXT RESOURCE 39 http://example.com/hello-mars.txt

Slide 40

Slide 40 text

RESOURCE DOES NOT EXIST 40 http://example.com/hello-pluto.txt

Slide 41

Slide 41 text

RESOURCE DOES NOT EXIST 41 GET /hello-pluto.txt HTTP/1.1 Host: example.com HTTP Request

Slide 42

Slide 42 text

RESOURCE DOES NOT EXIST 42 HTTP/1.1 404 Not Found HTTP Response

Slide 43

Slide 43 text

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 ▫︎…

Slide 44

Slide 44 text

HTTP VERBS 44 ▫︎GET ▫︎POST ▫︎PUT ▫︎DELETE ▫︎HEAD ▫︎OPTIONS

Slide 45

Slide 45 text

HTTP STATUS CODES 45 ▫︎Success (2xx) ▫︎200 OK ▫︎201 Created ▫︎204 No Content ▫︎…

Slide 46

Slide 46 text

HTTP STATUS CODES 46 ▫︎Client Error (4xx) ▫︎400 Bad Request ▫︎404 Not Found ▫︎409 Conflict ▫︎…

Slide 47

Slide 47 text

HTTP STATUS CODES 47 ▫︎Server Error (5xx) ▫︎500 Internal Server Error ▫︎503 Server Unavailable ▫︎…

Slide 48

Slide 48 text

THE RESOURCE-ORIENTED ARCHITECTURE 48

Slide 49

Slide 49 text

REST 49 Representational State Transfer

Slide 50

Slide 50 text

REST 50 HTTP + Resource-Oriented Architecture

Slide 51

Slide 51 text

REST 51 HTTP + Resource-Oriented Architecture RESTful

Slide 52

Slide 52 text

EMPLOYEE RESOURCE 52

Slide 53

Slide 53 text

EMPLOYEE RESOURCE 53 ▫︎Alice ▫︎Developer ▫︎Female ▫︎…

Slide 54

Slide 54 text

XML REPRESENTATION 54 Alice Developer female

Slide 55

Slide 55 text

JSON REPRESENTATION 55 { "name": "Alice", "role": "Developer", "gender": "female" }

Slide 56

Slide 56 text

HTML REPRESENTATION 56

Alice

Role:
Developer
Gender:
Female

Slide 57

Slide 57 text

EMPLOYEE RESOURCE 57 /employees

Slide 58

Slide 58 text

EMPLOYEE RESOURCE 58 /employees/alice /employees/bob /employees/eve

Slide 59

Slide 59 text

RESOURCE OPERATIONS 59 ▫︎Create ▫︎Read ▫︎Update ▫︎Delete ▫︎List

Slide 60

Slide 60 text

LIST EMPLOYEE RESOURCES 60 GET /employees HTTP/1.1 Host: example.com Accept: application/xml HTTP Request

Slide 61

Slide 61 text

LIST EMPLOYEE RESOURCES 61 HTTP/1.1 200 OK Content-Type: application/xml HTTP Response

Slide 62

Slide 62 text

READ EMPLOYEE RESOURCE 62 GET /employees/alice HTTP/1.1 Host: example.com Accept: application/xml HTTP Request

Slide 63

Slide 63 text

READ EMPLOYEE RESOURCE 63 HTTP/1.1 200 OK Content-Type: application/xml Alice Developer female HTTP Response

Slide 64

Slide 64 text

CREATE EMPLOYEE RESOURCE 64 POST /employees HTTP/1.1 Host: example.com Content-Type: application/xml John QA male HTTP Request

Slide 65

Slide 65 text

CREATE EMPLOYEE RESOURCE 65 HTTP/1.1 201 Created Location: /employees/john HTTP Response

Slide 66

Slide 66 text

UPDATE EMPLOYEE RESOURCE 66 PUT /employees/alice HTTP/1.1 Host: example.com Content-Type: application/xml Alice Manager female HTTP Request

Slide 67

Slide 67 text

UPDATE EMPLOYEE RESOURCE 67 HTTP/1.1 200 OK HTTP Response

Slide 68

Slide 68 text

DELETE EMPLOYEE RESOURCE 68 DELETE /employees/alice HTTP/1.1 Host: example.com HTTP Request

Slide 69

Slide 69 text

DELETE EMPLOYEE RESOURCE 69 HTTP/1.1 204 No Content HTTP Response

Slide 70

Slide 70 text

RESOURCE-ORIENTED ARCHITECTURE 70 1. Addressability 2. Statelessness 3. Connectedness 4. Uniform Interface

Slide 71

Slide 71 text

ADDRESSABILITY 71 Every interesting piece of information the server can provide should be exposed as a resource, and given its own URI

Slide 72

Slide 72 text

ADDRESSABILITY 72 http://example.com/employees/alice

Slide 73

Slide 73 text

STATELESSNESS 73 Every HTTP request should happen in complete isolation

Slide 74

Slide 74 text

STATELESSNESS 74 http://google.com/search?q=jellyfish

Slide 75

Slide 75 text

STATELESSNESS 75

Slide 76

Slide 76 text

STATELESSNESS 76

Slide 77

Slide 77 text

STATELESSNESS 77 http://google.com/search? q=jellyfish&start=10

Slide 78

Slide 78 text

STATELESSNESS 78 Application State vs. Resource State

Slide 79

Slide 79 text

CONNECTEDNESS 79 Documents should contain not just data, but links to other resources

Slide 80

Slide 80 text

CONNECTEDNESS 80

Slide 81

Slide 81 text

CONNECTEDNESS 81

Slide 82

Slide 82 text

CONNECTEDNESS 82

Slide 83

Slide 83 text

CONNECTEDNESS 83 { "employees": [ "/employees/alice", "/employees/bob", "/employees/eve", ... ] "next_page": "/employees?start=10", "create_employee": "/employees" }

Slide 84

Slide 84 text

HATEOAS 84 Hypermedia As The Engine of Application State

Slide 85

Slide 85 text

UNIFORM INTERFACE 85 ▫︎Create: POST /employees ▫︎Read: GET /employees/alice ▫︎Update: PUT /employees/alice ▫︎Delete: DELETE /employees/alice ▫︎List: GET /employees

Slide 86

Slide 86 text

UNIFORM INTERFACE 86 ▫︎Create: POST /resource ▫︎Read: GET /resource/{name} ▫︎Update: PUT /resource/{name} ▫︎Delete: DELETE /resource/{name} ▫︎List: GET /resource

Slide 87

Slide 87 text

SAFETY 87 GET and HEAD never change the resource state

Slide 88

Slide 88 text

INDEMPOTENCE 88 PUT and DELETE are indempotent

Slide 89

Slide 89 text

RESTFUL WEB SERVICES 89

Slide 90

Slide 90 text

WEB SERVICES 90 client server Web

Slide 91

Slide 91 text

BIG WEB SERVICES 91 ▫︎Heavy ▫︎Don’t scale ▫︎Hard to understand ▫︎Tight coupling ▫︎SOAP, WSDL, etc…

Slide 92

Slide 92 text

TIGHT COUPLING 92

Slide 93

Slide 93 text

BROKEN TIGHT COUPLING 93

Slide 94

Slide 94 text

RESTFUL WEB SERVICES 94 ▫︎Lightweight ▫︎Cacheable ▫︎Scalable ▫︎Discoverable ▫︎Loose coupling

Slide 95

Slide 95 text

RESOURCE-ORIENTED ARCHITECTURE 95 1. Addressability 2. Statelessness 3. Connectedness 4. Uniform Interface

Slide 96

Slide 96 text

CACHEABILITY 96 GET http://example.com/employees/alice

Slide 97

Slide 97 text

CACHEABILITY 97 GET http://example.com/employees/alice

Slide 98

Slide 98 text

SCALABILITY 98 GET http://example.com/employees/alice client server

Slide 99

Slide 99 text

SCALABILITY 99 GET http://example.com/employees/alice client server cluster

Slide 100

Slide 100 text

DISCOVERABILITY 100

Slide 101

Slide 101 text

DISCOVERABILITY 101 { "employees": [ "/employees/alice", "/employees/bob", "/employees/eve", ... ] "next_page": "/employees?start=10", "create_employee": "/employees" }

Slide 102

Slide 102 text

PUBLIC RESTFUL APIS 102 ▫︎Twitter ▫︎GitHub ▫︎Amazon S3

Slide 103

Slide 103 text

REFERENCE 103 RESTful Web Services Leonard Richardson Sam Ruby

Slide 104

Slide 104 text

Felipe Dornelas [email protected] THANK YOU