Slide 1

Slide 1 text

Ross Tuck HTTP And Your Angry Dog April 13th, Whisky Web

Slide 2

Slide 2 text

Who Am I?

Slide 3

Slide 3 text

Ross Tuck

Slide 4

Slide 4 text

Team Lead at Ibuildings Codemonkey Token Foreigner REST nut

Slide 5

Slide 5 text

@rosstuck

Slide 6

Slide 6 text

Today's topic:

Slide 7

Slide 7 text

Dogs

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

HTTP & Dogs

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

The Agenda

Slide 18

Slide 18 text

Basics

Slide 19

Slide 19 text

Client Server Request Response

Slide 20

Slide 20 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request

Slide 21

Slide 21 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request 2 Parts

Slide 22

Slide 22 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request The body

Slide 23

Slide 23 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 My application ... Request The body

Slide 24

Slide 24 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request The body

Slide 25

Slide 25 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request

Slide 26

Slide 26 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request The headers

Slide 27

Slide 27 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request The good stuff

Slide 28

Slide 28 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request

Slide 29

Slide 29 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request GET, POST, PUT, DELETE

Slide 30

Slide 30 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request Relative URL HTTP version

Slide 31

Slide 31 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request Key/Value pairs

Slide 32

Slide 32 text

POST /gists HTTP/1.1 Authorization: Basic xxxxxxxx Host: api.github.com Content-Length: 146 { "description": "the description for this gist", "public": false, "files": { ... Request

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

HTTP/1.1 201 Created Date: Sun, 09 Sep 2012 11:42:41 GMT Content-Length: 1848 Location: https://api.github.com/gists/a43a0cf58 { "description": "the description for this gist", "comments": 0, "created_at": "2012-09-09T11:42:40Z", ... Response Status code

Slide 35

Slide 35 text

Status Codes

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

• 2xx • 3xx • 4xx • 5xx OK! Over there! Client screwed up! Server screwed up!

Slide 39

Slide 39 text

Content Negotiation

Slide 40

Slide 40 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Request

Slide 41

Slide 41 text

HTTP/1.1 200 OK Date: Sun, 26 Aug 2012 18:00:43 GMT { "cute": true, "big": false, "data_dog": true } Response

Slide 42

Slide 42 text

HTTP/1.1 200 OK Date: Sun, 26 Aug 2012 18:00:43 GMT true Response

Slide 43

Slide 43 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Request

Slide 44

Slide 44 text

GET /dogs/corgi.json HTTP/1.1 Host: api.example.com Request

Slide 45

Slide 45 text

/dogs/corgi.json !== /dogs/corgi.xml

Slide 46

Slide 46 text

Imagine the URL as your primary key.

Slide 47

Slide 47 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Request

Slide 48

Slide 48 text

GET /dogs/corgi?_format=json HTTP/1.1 Host: api.example.com Request

Slide 49

Slide 49 text

POST /dogs/corgi?_format=json HTTP/1.1 Host: api.example.com Request

Slide 50

Slide 50 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Request

Slide 51

Slide 51 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Accept: application/json Request

Slide 52

Slide 52 text

More POWAH

Slide 53

Slide 53 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Accept: application/json Request

Slide 54

Slide 54 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Accept: application/json, application/xml Request How do I choose?

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Accept: application/json, application/xml Request

Slide 57

Slide 57 text

HTTP/1.1 200 OK Date: Sun, 26 Aug 2012 18:00:43 GMT Content-Type: application/json { "cute": true, "big": false, "data_dog": true } Response

Slide 58

Slide 58 text

Nifty.

Slide 59

Slide 59 text

GET /dogs/corgi HTTP/1.1 Host: api.example.com Accept: application/json, application/xml Request

Slide 60

Slide 60 text

text/html, text/plain

Slide 61

Slide 61 text

text/html;key=value, text/plain

Slide 62

Slide 62 text

text/html;key=value;foo=bar, text/plain

Slide 63

Slide 63 text

text/html, text/plain

Slide 64

Slide 64 text

text/html, text/plain;q=0.5 Quality (Default 1.0)

Slide 65

Slide 65 text

text/html, text/plain;q=0.5, text/*;q=0.1 Wildcards

Slide 66

Slide 66 text

text/html, text/plain;q=0.5, */*;q=0.1 Anything at all

Slide 67

Slide 67 text

Accept Headers Little weird...

Slide 68

Slide 68 text

But not so scary.

Slide 69

Slide 69 text

text/html,application/xhtml+xml, application/xml;q=0.9,*/*;q=0.8

Slide 70

Slide 70 text

Cool...

Slide 71

Slide 71 text

What the heck is it good for?

Slide 72

Slide 72 text

Accept is a “Pattern”

Slide 73

Slide 73 text

Accept-Language Accept-Encoding Accept-Charset Accept-Ranges

Slide 74

Slide 74 text

Content-Language Content-Encoding (works differently) Content-Range

Slide 75

Slide 75 text

Resource vs Representation /dog/corgi JSON, Dutch, Gzipped, /dog/corgi

Slide 76

Slide 76 text

Resource vs Representation

Slide 77

Slide 77 text

Best way to version your API. Arguably. Right now.

Slide 78

Slide 78 text

/v1/dogs/corgi

Slide 79

Slide 79 text

Accept: application/vnd.dogipedia-v1+json

Slide 80

Slide 80 text

Accept: application/vnd.dogipedia-v2+json

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

Vary

Slide 84

Slide 84 text

Client Server GET /dogs/corgi HTTP/1.1 Host: api.example.com

Slide 85

Slide 85 text

Client Server GET /dogs/corgi HTTP/1.1 Host: api.example.com

Slide 86

Slide 86 text

Client Server Proxy GET /dogs/corgi HTTP/1.1 Host: api.example.com

Slide 87

Slide 87 text

Client Server Proxy GET /dogs/corgi HTTP/1.1 Host: api.example.com Accept: application/json, text/plain User-Species: cat

Slide 88

Slide 88 text

Same URL. Different output. WTF should I return?

Slide 89

Slide 89 text

Client Server Proxy GET /dogs/corgi HTTP/1.1 Host: api.example.com Accept: application/json, text/plain User-Species: cat

Slide 90

Slide 90 text

Client Server Proxy

Slide 91

Slide 91 text

Here's how. Hint: Involves the Vary header!

Slide 92

Slide 92 text

Client Server Proxy /dogs/corgi Accept: application/json, text/plain User-Species: cat

Slide 93

Slide 93 text

HTTP/1.1 200 OK Date: Sun, 26 Aug 2012 18:00:43 GMT Content-Type: application/json Vary: Accept {“json”: “omgz”} Response

Slide 94

Slide 94 text

Client Server Proxy URL and Accept? Okay, I got this.

Slide 95

Slide 95 text

Some time later...

Slide 96

Slide 96 text

Client Server Proxy /dogs/corgi Accept: application/json, text/plain User-Species: aardvark

Slide 97

Slide 97 text

Client Server Proxy Valid cache. I has it.

Slide 98

Slide 98 text

Client Server Proxy ZZ Z Z Z Z

Slide 99

Slide 99 text

HTTP/1.1 200 OK Date: Sun, 26 Aug 2012 18:00:43 GMT Content-Type: application/json Vary: Accept {“json”: “omgz”} Response

Slide 100

Slide 100 text

HTTP/1.1 200 OK Date: Sun, 26 Aug 2012 18:00:43 GMT Content-Type: application/json Vary: Accept, User-Species {“json”: “omgz”} Response

Slide 101

Slide 101 text

HTTP/1.1 200 OK Date: Sun, 26 Aug 2012 18:00:43 GMT Content-Type: application/json Vary: Accept, User-Species {“json”: “dogs rule, cats drool”} Response

Slide 102

Slide 102 text

Request headers. Not Response!

Slide 103

Slide 103 text

Bad Reputation? 2 Reasons

Slide 104

Slide 104 text

1. Accept-Encoding -Language

Slide 105

Slide 105 text

2. Internet Explorer

Slide 106

Slide 106 text

Caching

Slide 107

Slide 107 text

Expires Pragma Cache-Control

Slide 108

Slide 108 text

Expires Pragma Cache-Control

Slide 109

Slide 109 text

Expires Cache-Control HTTP 1.0 HTTP 1.1

Slide 110

Slide 110 text

HTTP/1.1 200 OK Expires: Thu, 07 Feb 2013 22:00:00 GMT {“herp”: “derp”} Response

Slide 111

Slide 111 text

HTTP/1.1 200 OK Cache-Control: max-age=120 {“herp”: “derp”} Response

Slide 112

Slide 112 text

HTTP/1.1 200 OK Cache-Control: max-age=120 Response

Slide 113

Slide 113 text

Expires Cache-Control HTTP 1.0 HTTP 1.1

Slide 114

Slide 114 text

HTTP/1.1 200 OK Cache-Control: max-age=120 {“herp”: “derp”} Response

Slide 115

Slide 115 text

HTTP/1.1 200 OK Cache-Control: max-age=120, s-maxage=120 {“herp”: “derp”} Response Dude, Where's my dash?

Slide 116

Slide 116 text

public private no-store no-cache no-transform must-revalidate proxy-revalidate

Slide 117

Slide 117 text

Mark Nottingham's Caching Tutorial http://www.mnot.net/cache_docs/ Much better than me.

Slide 118

Slide 118 text

Conditional Requests

Slide 119

Slide 119 text

Conditional Requests

Slide 120

Slide 120 text

The Part About ETags

Slide 121

Slide 121 text

Conditional Requests

Slide 122

Slide 122 text

DELETE /ross/reputation HTTP/1.1 Host: api.joind.in If-Talk-Quality: Crap Request

Slide 123

Slide 123 text

if ($talkQuality === 'Crap') { delete($rossReputation); } Not real code Server

Slide 124

Slide 124 text

What kind of conditions?

Slide 125

Slide 125 text

If-Match If-None-Match If-Modified-Since If-Unmodified-Since If-Range ETags Datetimes Either

Slide 126

Slide 126 text

Wait a second, Ross. Audience

Slide 127

Slide 127 text

What the heck is an ETag, anyways?

Slide 128

Slide 128 text

A string. Any string.

Slide 129

Slide 129 text

One rule:

Slide 130

Slide 130 text

Represent the current state.

Slide 131

Slide 131 text

“14” “a381bedb5d4478053eb04be35f8798dd” “winnie-the-pooh”

Slide 132

Slide 132 text

...for the current representation.

Slide 133

Slide 133 text

etag(“v14-json-en”) !== etag(“v14-xml-en”) Don't cross the streams Server

Slide 134

Slide 134 text

Last Modified Date sounds easier... Audience

Slide 135

Slide 135 text

Wed, 15 Nov 1995 04:58:08 GMT One second of precision

Slide 136

Slide 136 text

Caching With Conditionals

Slide 137

Slide 137 text

Use Case

Slide 138

Slide 138 text

GET /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* Request

Slide 139

Slide 139 text

HTTP/1.1 200 OK Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Vary: Accept ETag: "f4e15911542b92b44bb38186e71cc8f5" "history": [ { "version": "529f6311d5518977534b6e1fd313...", ... Response

Slide 140

Slide 140 text

... "user": { "gravatar_id": "c26bfcbd5f786591e036fa0", "avatar_url": "https://secure.gravatar...", "login": "rosstuck", "url": "https://api.github.com/users/rosstuck", "id": 146766 }, "change_status": { "additions": 1, "deletions": 0, "total": 1 }, Response

Slide 141

Slide 141 text

"url": "https://api.github.com/gists/348...", "committed_at": "2012-08-26T17:40:03Z" } ], "git_pull_url": "git://gist.github.com/34819...", "forks": [ ], "html_url": "https://gist.github.com/3481910", "git_push_url": "[email protected]:3481910.git", "comments": 0, "user": { Response

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

HTTP/1.1 200 OK Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Vary: Accept ETag: "f4e15911542b92b44bb38186e71cc8f5" { "history": [ { "version": "529f6311d5518970903cb5427534b6e1fd313aca", "user": { "gravatar_id": "c26bfcbd5f786591e036fa0958a11e8b", "avatar_url": "https://secure.gravatar.com/avatar/c26bfcbd5f786591e036fa0958a11e8b?d=https://a2... "login": "rosstuck", "url": "https://api.github.com/users/rosstuck", "id": 146766 }, "change_status": { "additions": 1, "deletions": 0, "total": 1 }, "url": "https://api.github.com/gists/3481910/529f6311d5518970903cb5427534b6e1fd313aca", "committed_at": "2012-08-26T17:40:03Z" } ], "git_pull_url": "git://gist.github.com/3481910.git", Response "forks": [ ], "html_url": "https://gist.github.com/3481910", "git_push_url": "[email protected]:3481910.git", "comments": 0, "user": { "gravatar_id": "c26bfcbd5f786591e036fa0958a11e8b", "avatar_url": "https://secure.gravatar.com/avatar/c26bfcbd5f78659....",} "login": "rosstuck", "url": "https://api.github.com/users/rosstuck", "id": 146766 }, "public": true, "created_at": "2012-08-26T17:40:03Z", "files": { "gistfile1.txt": { "type": "text/plain", "filename": "gistfile1.txt", "raw_url": "https://gist.github.com/raw/3481910/8b6946739e8098408ee3af96... "content": "Hello PFC!", "language": null, "size": 10 } }, "description": "", "url": "https://api.github.com/gists/3481910", "updated_at": "2012-08-26T17:40:03Z", "id": "3481910" }

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

GET /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-None-Match: "f4e15911542b92b44bb38186e71cc8f5" Request

Slide 146

Slide 146 text

HTTP/1.1 304 Not Modified Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Vary: Accept ETag: "f4e15911542b92b44bb38186e71cc8f5" Response

Slide 147

Slide 147 text

HTTP/1.1 304 Not Modified Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Vary: Accept ETag: "f4e15911542b92b44bb38186e71cc8f5" Response

Slide 148

Slide 148 text

HTTP/1.1 304 Not Modified Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Vary: Accept ETag: "f4e15911542b92b44bb38186e71cc8f5" Response No giant body!

Slide 149

Slide 149 text

Caching. You has it.

Slide 150

Slide 150 text

GET /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-None-Match: "a381bedb5d4478053eb04be35f8798dd" Request

Slide 151

Slide 151 text

GET /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-None-Match: "ross-is-a-poo-poo-head" Request

Slide 152

Slide 152 text

HTTP/1.1 200 OK Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Vary: Accept ETag: "f4e15911542b92b44bb38186e71cc8f5" "history": [ { "version": "529f6311d5518977534b6e1fd313...", Response

Slide 153

Slide 153 text

Recap

Slide 154

Slide 154 text

No ETag Old ETag Matching ETag Full Body Full Body No Body → → →

Slide 155

Slide 155 text

...on supported servers.

Slide 156

Slide 156 text

Why?

Slide 157

Slide 157 text

Parsing Bandwidth Response time Probably ...Maybe

Slide 158

Slide 158 text

However...

Slide 159

Slide 159 text

“The fastest request is one you don't make.” - Jesus

Slide 160

Slide 160 text

More Fun With ETags

Slide 161

Slide 161 text

Optimistic Concurrency Control “Record Versioning”

Slide 162

Slide 162 text

Request

Slide 163

Slide 163 text

GET /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-None-Match: "f4e15911542b92b44bb38186e71cc8f5" Request

Slide 164

Slide 164 text

PATCH /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-None-Match: "f4e15911542b92b44bb38186e71cc8f5" Request

Slide 165

Slide 165 text

PATCH /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-Match: "f4e15911542b92b44bb38186e71cc8f5" Request

Slide 166

Slide 166 text

PATCH /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-Match: "f4e15911542b92b44bb38186e71cc8f5" { "description": "cheese om nom nom" } Request

Slide 167

Slide 167 text

Response

Slide 168

Slide 168 text

Response HTTP/1.1 200 OK Server: nginx/1.0.13 Date: Sat, 01 Sep 2012 14:01:38 GMT ETag: "899b76047a5e68445668374c2e0faa32" { "description": "cheese om nom nom", "user": { "login": "rosstuck", ...

Slide 169

Slide 169 text

It works.

Slide 170

Slide 170 text

So what?

Slide 171

Slide 171 text

What if I send something...

Slide 172

Slide 172 text

No content

Slide 173

Slide 173 text

PATCH /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-Match: "899b76047a5e68445668374c2e0faa32" { "description": "cheese om nom nom" } Request

Slide 174

Slide 174 text

PATCH /gists/3481910 HTTP/1.1 Host: api.github.com Accept: */* If-Match: "stay-puft-marshmellow-dog!" { "description": "cheese om nom nom" } Request

Slide 175

Slide 175 text

HTTP/1.1 412 Precondition Failed Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Response

Slide 176

Slide 176 text

Response

Slide 177

Slide 177 text

if (“stay-puft-marshmellow-dog” == “f4e1591..”) { patchTheRecord(); } Server

Slide 178

Slide 178 text

if (“stay-puft-marshmellow-dog” == “f4e1591..”) { patchTheRecord(); } else { sendScary412Message(); } Server

Slide 179

Slide 179 text

Your ETag is out of date.

Slide 180

Slide 180 text

“Two guys on the same record” problem

Slide 181

Slide 181 text

Other Scary Precondition Errors

Slide 182

Slide 182 text

Disclaimer New Stuff Ahead

Slide 183

Slide 183 text

DELETE /gists/3481910 HTTP/1.1 Host: api.github.com Request

Slide 184

Slide 184 text

HTTP/1.1 428 Precondition Required Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Response

Slide 185

Slide 185 text

Am I operating on the latest version?

Slide 186

Slide 186 text

DELETE /gists/3481910 HTTP/1.1 Host: api.github.com Request

Slide 187

Slide 187 text

DELETE /gists/3481910 HTTP/1.1 Host: api.github.com If-Match: "f4e15911542b92b44bb38186e71cc8f5" Request

Slide 188

Slide 188 text

HTTP/1.1 204 No Content Server: nginx/1.0.13 Date: Sun, 26 Aug 2012 18:00:43 GMT Response

Slide 189

Slide 189 text

Look before you leap.

Slide 190

Slide 190 text

Tooling

Slide 191

Slide 191 text

No content

Slide 192

Slide 192 text

No content

Slide 193

Slide 193 text

No content

Slide 194

Slide 194 text

Epilogue: HTTP & Dogs

Slide 195

Slide 195 text

Content Negotiation Vary Caching Preconditions

Slide 196

Slide 196 text

Treat it like your framework.

Slide 197

Slide 197 text

No content