Introduction to RESTful Web Services

104
an introduction to RESTFUL WEB SERVICES Felipe Dornelas

Transcript of Introduction to RESTful Web Services

Page 1: Introduction to RESTful Web Services

a n i n t r o d u c t i o n t o

RESTFUL WEB SERVICESFelipe Dornelas

Page 2: Introduction to RESTful Web Services

AGENDA

2

▫︎The Internet

▫︎The Web and its Resources

▫︎HTTP

▫︎The Resource-Oriented Architecture

▫︎RESTful Web Services

Page 3: Introduction to RESTful Web Services

WHAT IS REST?

3

HTTP + Resource-Oriented Architecture

Page 4: Introduction to RESTful Web Services

THE INTERNETA network of networks

4

Page 5: Introduction to RESTful Web Services

5

Page 6: Introduction to RESTful Web Services

6

Page 7: Introduction to RESTful Web Services

THE INTERNET, 2010

7

Page 8: Introduction to RESTful Web Services

INTERNET ROUTES

8

Page 9: Introduction to RESTful Web Services

INTERNET ROUTES

9

Page 10: Introduction to RESTful Web Services

CACHING

10

Page 11: Introduction to RESTful Web Services

INTERNET LAYERS

11

Web, E-mail, BitTorrent, DNS…

TCP, UDP…

Internet Protocol (IP)

WiFi, Ethernet, 3G, LTE…

Page 12: Introduction to RESTful Web Services

INTERNET LAYERS

12

We will talk about the Web

Page 13: Introduction to RESTful Web Services

THE WEBAn application of the Internet

13

Page 14: Introduction to RESTful Web Services

WHAT IS THE WEB?

14

An information system of interlinked hypertext documents and resources

accessed via the Internet

Page 15: Introduction to RESTful Web Services

HYPERTEXT DOCUMENTS

15

Page 16: Introduction to RESTful Web Services

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>

Page 17: Introduction to RESTful Web Services

HYPERTEXT TRANSFER PROTOCOL

17

ServerClient

example.comMozilla Firefox

Page 18: Introduction to RESTful Web Services

HYPERTEXT TRANSFER PROTOCOL

18

ServerClient

HTTP Requestexample.comMozilla Firefox

Page 19: Introduction to RESTful Web Services

HTTP REQUEST

19

GET / HTTP/1.1User-Agent: Mozilla FirefoxHost: example.comAccept: */*

Page 20: Introduction to RESTful Web Services

HYPERTEXT TRANSFER PROTOCOL

20

ServerClient

HTTP Responseexample.comMozilla Firefox

Page 21: Introduction to RESTful Web Services

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>

Page 22: Introduction to RESTful Web Services

22

Page 23: Introduction to RESTful Web Services

INTERNET LAYERS

23

HTTP

TCP

Internet Protocol (IP)

WiFi, Ethernet, 3G, LTE…

Page 24: Introduction to RESTful Web Services

RESOURCES

24

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

Page 25: Introduction to RESTful Web Services

RESOURCES

25

▫︎Can be concrete things:

▫︎Web pages

▫︎Files

▫︎Videos

▫︎Blog posts

▫︎Articles

Page 26: Introduction to RESTful Web Services

RESOURCES

26

▫︎Can also represent abstract concepts:

▫︎Employees in a enterprise

▫︎Money transfers

▫︎Products in a online store

▫︎Calendar appointments

▫︎User accounts

Page 27: Introduction to RESTful Web Services

RESOURCE NAMES

27

▫︎URN - Uniform Resource Name

▫︎products/54321

▫︎about-us

▫︎articles/web.html

▫︎posts/2015-04-13

▫︎podcasts/rest.mp3

Page 28: Introduction to RESTful Web Services

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

Page 29: Introduction to RESTful Web Services

ANATOMY OF AN URL

29

Page 30: Introduction to RESTful Web Services

RESOURCE IDENTIFIERS

30

Page 31: Introduction to RESTful Web Services

RESOURCE IDENTIFIERS

31

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

Page 32: Introduction to RESTful Web Services

RESOURCES

32

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

Page 33: Introduction to RESTful Web Services

Imaginary HTTP server:

example.com

33

Page 34: Introduction to RESTful Web Services

READING A TEXT RESOURCE

34

http://example.com/hello-world.txt

Page 35: Introduction to RESTful Web Services

READING A TEXT RESOURCE

35

GET /hello-world.txt HTTP/1.1Host: example.com

HTTP Request

Page 36: Introduction to RESTful Web Services

READING A TEXT RESOURCE

36

HTTP/1.1 200 OKContent-Type: text/plainContent-Length: 13

Hello, World!

HTTP Response

Page 37: Introduction to RESTful Web Services

CREATING A TEXT RESOURCE

37

POST / HTTP/1.1Host: example.comContent-Type: text/plain

Hello, Mars!

HTTP Request

Page 38: Introduction to RESTful Web Services

CREATING A TEXT RESOURCE

38

HTTP/1.1 201 CreatedLocation: /hello-mars.txt

HTTP Response

Page 39: Introduction to RESTful Web Services

CREATING A TEXT RESOURCE

39

http://example.com/hello-mars.txt

Page 40: Introduction to RESTful Web Services

RESOURCE DOES NOT EXIST

40

http://example.com/hello-pluto.txt

Page 41: Introduction to RESTful Web Services

RESOURCE DOES NOT EXIST

41

GET /hello-pluto.txt HTTP/1.1Host: example.com

HTTP Request

Page 42: Introduction to RESTful Web Services

RESOURCE DOES NOT EXIST

42

HTTP/1.1 404 Not Found

HTTP Response

Page 43: Introduction to RESTful Web Services

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

▫︎…

Page 44: Introduction to RESTful Web Services

HTTP VERBS

44

▫︎GET

▫︎POST

▫︎PUT

▫︎DELETE

▫︎HEAD

▫︎OPTIONS

Page 45: Introduction to RESTful Web Services

HTTP STATUS CODES

45

▫︎Success (2xx)

▫︎200 OK

▫︎201 Created

▫︎204 No Content

▫︎…

Page 46: Introduction to RESTful Web Services

HTTP STATUS CODES

46

▫︎Client Error (4xx)

▫︎400 Bad Request

▫︎404 Not Found

▫︎409 Conflict

▫︎…

Page 47: Introduction to RESTful Web Services

HTTP STATUS CODES

47

▫︎Server Error (5xx)

▫︎500 Internal Server Error

▫︎503 Server Unavailable

▫︎…

Page 48: Introduction to RESTful Web Services

THE RESOURCE-ORIENTED ARCHITECTURE

48

Page 49: Introduction to RESTful Web Services

REST

49

Representational State Transfer

Page 50: Introduction to RESTful Web Services

REST

50

HTTP + Resource-Oriented Architecture

Page 51: Introduction to RESTful Web Services

REST

51

HTTP + Resource-Oriented Architecture

RESTful

Page 52: Introduction to RESTful Web Services

EMPLOYEE RESOURCE

52

Page 53: Introduction to RESTful Web Services

EMPLOYEE RESOURCE

53

▫︎Alice

▫︎Developer

▫︎Female

▫︎…

Page 54: Introduction to RESTful Web Services

XML REPRESENTATION

54

<employee> <name>Alice</name> <role>Developer</role> <gender>female</gender></employee>

Page 55: Introduction to RESTful Web Services

JSON REPRESENTATION

55

{ "name": "Alice", "role": "Developer", "gender": "female"}

Page 56: Introduction to RESTful Web Services

HTML REPRESENTATION

56

<h1>Alice</h1><dl><dt>Role:</dt> <dd>Developer</dd><dt>Gender:</dt> <dd>Female</dd></dl>

Page 57: Introduction to RESTful Web Services

EMPLOYEE RESOURCE

57

/employees

Page 58: Introduction to RESTful Web Services

EMPLOYEE RESOURCE

58

/employees/alice

/employees/bob

/employees/eve

Page 59: Introduction to RESTful Web Services

RESOURCE OPERATIONS

59

▫︎Create

▫︎Read

▫︎Update

▫︎Delete

▫︎List

Page 60: Introduction to RESTful Web Services

LIST EMPLOYEE RESOURCES

60

GET /employees HTTP/1.1Host: example.comAccept: application/xml

HTTP Request

Page 61: Introduction to RESTful Web Services

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

Page 62: Introduction to RESTful Web Services

READ EMPLOYEE RESOURCE

62

GET /employees/alice HTTP/1.1Host: example.comAccept: application/xml

HTTP Request

Page 63: Introduction to RESTful Web Services

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

Page 64: Introduction to RESTful Web Services

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

Page 65: Introduction to RESTful Web Services

CREATE EMPLOYEE RESOURCE

65

HTTP/1.1 201 CreatedLocation: /employees/john

HTTP Response

Page 66: Introduction to RESTful Web Services

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

Page 67: Introduction to RESTful Web Services

UPDATE EMPLOYEE RESOURCE

67

HTTP/1.1 200 OK

HTTP Response

Page 68: Introduction to RESTful Web Services

DELETE EMPLOYEE RESOURCE

68

DELETE /employees/alice HTTP/1.1Host: example.com

HTTP Request

Page 69: Introduction to RESTful Web Services

DELETE EMPLOYEE RESOURCE

69

HTTP/1.1 204 No Content

HTTP Response

Page 70: Introduction to RESTful Web Services

RESOURCE-ORIENTED ARCHITECTURE

70

1. Addressability

2. Statelessness

3. Connectedness

4. Uniform Interface

Page 71: Introduction to RESTful Web Services

ADDRESSABILITY

71

Every interesting piece of information the server can provide should be exposed as a resource,

and given its own URI

Page 72: Introduction to RESTful Web Services

ADDRESSABILITY

72

http://example.com/employees/alice

Page 73: Introduction to RESTful Web Services

STATELESSNESS

73

Every HTTP request should happen in complete isolation

Page 74: Introduction to RESTful Web Services

STATELESSNESS

74

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

Page 75: Introduction to RESTful Web Services

STATELESSNESS

75

Page 76: Introduction to RESTful Web Services

STATELESSNESS

76

Page 77: Introduction to RESTful Web Services

STATELESSNESS

77

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

Page 78: Introduction to RESTful Web Services

STATELESSNESS

78

Application State vs. Resource State

Page 79: Introduction to RESTful Web Services

CONNECTEDNESS

79

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

Page 80: Introduction to RESTful Web Services

CONNECTEDNESS

80

Page 81: Introduction to RESTful Web Services

CONNECTEDNESS

81

Page 82: Introduction to RESTful Web Services

CONNECTEDNESS

82

Page 83: Introduction to RESTful Web Services

CONNECTEDNESS

83

{ "employees": [ "/employees/alice", "/employees/bob", "/employees/eve", ... ]

"next_page": "/employees?start=10", "create_employee": "/employees"}

Page 84: Introduction to RESTful Web Services

HATEOAS

84

Hypermedia As The Engine of Application State

Page 85: Introduction to RESTful Web Services

UNIFORM INTERFACE

85

▫︎Create: POST /employees

▫︎Read: GET /employees/alice

▫︎Update: PUT /employees/alice

▫︎Delete: DELETE /employees/alice

▫︎List: GET /employees

Page 86: Introduction to RESTful Web Services

UNIFORM INTERFACE

86

▫︎Create: POST /resource

▫︎Read: GET /resource/{name}

▫︎Update: PUT /resource/{name}

▫︎Delete: DELETE /resource/{name}

▫︎List: GET /resource

Page 87: Introduction to RESTful Web Services

SAFETY

87

GET and HEAD never change the resource state

Page 88: Introduction to RESTful Web Services

INDEMPOTENCE

88

PUT and DELETE are indempotent

Page 89: Introduction to RESTful Web Services

RESTFUL WEB SERVICES

89

Page 90: Introduction to RESTful Web Services

WEB SERVICES

90

client server

Web

Page 91: Introduction to RESTful Web Services

BIG WEB SERVICES

91

▫︎Heavy

▫︎Don’t scale

▫︎Hard to understand

▫︎Tight coupling

▫︎SOAP, WSDL, etc…

Page 92: Introduction to RESTful Web Services

TIGHT COUPLING

92

Page 93: Introduction to RESTful Web Services

BROKEN TIGHT COUPLING

93

Page 94: Introduction to RESTful Web Services

RESTFUL WEB SERVICES

94

▫︎Lightweight

▫︎Cacheable

▫︎Scalable

▫︎Discoverable

▫︎Loose coupling

Page 95: Introduction to RESTful Web Services

RESOURCE-ORIENTED ARCHITECTURE

95

1. Addressability

2. Statelessness

3. Connectedness

4. Uniform Interface

Page 96: Introduction to RESTful Web Services

CACHEABILITY

96

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

Page 97: Introduction to RESTful Web Services

CACHEABILITY

97

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

Page 98: Introduction to RESTful Web Services

SCALABILITY

98

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

clientserver

Page 99: Introduction to RESTful Web Services

SCALABILITY

99

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

client

server cluster

Page 100: Introduction to RESTful Web Services

DISCOVERABILITY

100

Page 101: Introduction to RESTful Web Services

DISCOVERABILITY

101

{ "employees": [ "/employees/alice", "/employees/bob", "/employees/eve", ... ]

"next_page": "/employees?start=10", "create_employee": "/employees"}

Page 102: Introduction to RESTful Web Services

PUBLIC RESTFUL APIS

102

▫︎Twitter

▫︎GitHub

▫︎Amazon S3

Page 103: Introduction to RESTful Web Services

REFERENCE

103

RESTful Web Services Leonard Richardson Sam Ruby

Page 104: Introduction to RESTful Web Services

Felipe Dornelas [email protected]

THANK YOU