Slide 1

Slide 1 text

HTTP OPTIONS And Self-Describing APIs

Slide 2

Slide 2 text

Lets the client discover what it may do to a resource Potentially describes how a client may use a resource Does not imply resource action or retrieval Response is not cacheable What is the OPTIONS method?

Slide 3

Slide 3 text

The Request

Slide 4

Slide 4 text

Use OPTIONS just like any other verb Provide any pertinent headers, e.g. authorization Request body (e.g. JSON payload) is optional HTTP/1.1 200 OK Allow: GET,HEAD,OPTIONS Content-Type: application/json {"tacos": ["fish", "steak", "veggie"]} OPTIONS /food/mexican HTTP/1.1 Accept: application/json Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

Slide 5

Slide 5 text

* (asterisk) corresponds to general server capabilities The response body may be apropos for a “site map” (HATEOAS!) HTTP/1.1 200 OK Allow: GET,HEAD,OPTIONS Content-Type: application/json {"tacos": ["fish", "steak", "veggie"]} OPTIONS * HTTP/1.1 Accept: application/json Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

Slide 6

Slide 6 text

The Response

Slide 7

Slide 7 text

Should respond with a 200 OK for a valid resource HTTP/1.1 200 OK Allow: GET,HEAD,OPTIONS Content-Type: application/json {"tacos": ["fish", "steak", "veggie"]} HTTP/1.1 200 OK Allow: GET,HEAD,OPTIONS Content-Type: application/json {"tacos": { "types": ["fish", "steak", "veggie"] }} response code

Slide 8

Slide 8 text

Indicate any optional features for the resource Allow: HTTP methods this resource allows headers HTTP/1.1 200 OK Allow: GET,HEAD,OPTIONS Content-Type: application/json {"tacos": ["fish", "steak", "veggie"]} HTTP/1.1 200 OK Allow: GET,HEAD,OPTIONS Content-Type: application/json {"tacos": { "types": ["fish", "steak", "veggie"] }}

Slide 9

Slide 9 text

HTTP/1.1 200 OK Allow: GET,HEAD,OPTIONS Content-Type: application/json {"tacos": { "types": ["fish", "steak", "veggie"] }} Optional, may include additional communication information for resource, i.e. documentation body

Slide 10

Slide 10 text

HTTP/1.1 200 OK Allow: DELETE,GET,HEAD,OPTIONS,PUT Content-Type: application/json {"tacos": { "types": ["fish", "steak", "veggie", "shrimp"] }} Response may change according to request. For example, allowing more methods after authenticating mutability

Slide 11

Slide 11 text

Who’s (not) using it? Hint: basically everybody

Slide 12

Slide 12 text

OPTIONS /users/zacstewart HTTP/1.1 Host: api.github.com Accept: */* HTTP/1.1 500 Internal Server Error GitHub curl -v -X OPTIONS https://api.github.com/users/zacstewart

Slide 13

Slide 13 text

wtf?

Slide 14

Slide 14 text

Twitter curl -v -X OPTIONS https://api.twitter.com/1/statuses/ public_timeline.json OPTIONS /1/statuses/ public_timeline.json HTTP/1.1 Host: api.twitter.com Accept: */* HTTP/1.1 403 Forbidden

Slide 15

Slide 15 text

@dache

Slide 16

Slide 16 text

How they could be

Slide 17

Slide 17 text

Service Overview (Site Map) Response body can indicate where your service’s resources live A good place to serve an ATOM service document, WADL schema, etc... OPTIONS * HTTP/1.1

Slide 18

Slide 18 text

Resource Description OPTIONS /repos/:user/:repo/issues/:number / HTTP/1.1 Permissions: the Allow header conveys how a user may use the resource Documentation describing how to use each of the available methods and what kind of output to expect can reside in the response body Partial WADL schema or JSON equivalent

Slide 19

Slide 19 text

Okay. So?

Slide 20

Slide 20 text

Increased Discoverability Better for API-crawling robots to index your web service Better for other web services that make use of your web service Means increased resilience

Slide 21

Slide 21 text

Less Glue Code Web services with an established convention for understanding one another means you spend less time making them play Horses will thank you. And unicorns.

Slide 22

Slide 22 text

Finally, some Ruby!

Slide 23

Slide 23 text

Rails As part of resourceful routes, extend CRUD to include member and collection options methods in the controller. But, until then...

Slide 24

Slide 24 text

zacstewart github.com/ twitter.com/ .com } https://github.com/RestDoc http://www.w3.org/Submission/wadl/