Upgrade to Pro — share decks privately, control downloads, hide ads and more …

HTTP OPTIONS Method And Self-Describing APIs

HTTP OPTIONS Method And Self-Describing APIs

Slide from my presentation at the ATLRUG on using the HTTP OPTIONS method making APIs more restful.

Zac Stewart

June 13, 2012
Tweet

More Decks by Zac Stewart

Other Decks in Programming

Transcript

  1. 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?
  2. 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==
  3. * (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==
  4. 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
  5. 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"] }}
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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.
  13. Rails As part of resourceful routes, extend CRUD to include

    member and collection options methods in the controller. But, until then...