The HTTP OPTIONS method and potential for self-describing RESTful APIs
zacstewart.com
zacstewart.com
Notice the OPTIONS button on the top right of the browse-able interface. For example:
$ curl -X OPTIONS http://rest.ep.io/pygments/
{"fields": { "lexer": "ChoiceField", "code": "CharField", "style": "ChoiceField", "linenos": "BooleanField", "title": "CharField"}, "parses": ["application/json", "application/x-www-form-urlencoded", "multipart/form-data", "application/xml", "application/yaml"], "renders": ["application/json", "application/json-p", "text/html", "application/xhtml+xml", "text/plain", "application/xml", "application/yaml"], "name": "Pygments Root", "description": "\nThis example demonstrates a simple RESTful Web API around the awesome pygments library.\nThis top level resource is used to create highlighted code snippets, and to list all the existing code snippets.\n" }
REST APIs must be self-describing from the start. Those kinds of responses should be sent for GET requests!
This is a must-read clarification from Roy, as most "REST" APIs are less RESTful than HTML pages built on top of them:
http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hyperte...
The part the interestes me most is documenting the parameters so that web services can better understand how to work with each other, instead of someone like me pouring over the docs and writing mundane glue code to tie Twitter search features into a service.
[1] https://developers.google.com/discovery/
[2] http://www.youtube.com/watch?v=nyu5ZxGUfgs (~35:30 is where they actually demonstrate building an API)
> Any effort spent describing what methods to use on what URIs of interest should be entirely defined within the scope of the processing rules for a media type (and, in most cases, already defined by existing media types).
What are the "processing rules" for JSON? Do they depend on the application? Do we have a standard?
My first thought is something like this,
{ "resources":
{ "foo":
{ href: 'http://example.com/foo',
method: 'get'
},
"bar":
{ href: 'http://example.com/bar',
method: 'post'
},
"baz":
{ href: 'http://example.com/baz',
method: 'put'
},
}
}There's something quite nice about being able to have a program intelligently build up knowledge of how to interact with an API, and be able to build a set of objects from a definition to facilitate that interaction. It's a serious weakness in non-XML APIs.
Full disclosure: I particularly do not like XML-based web services, so please don't misread this as a cheap shot at JSON APIs. I prefer JSON, but programming against APIs would be so much nicer if there were a WSDL for JSON APIs.
http://lists.w3.org/Archives/Public/www-tag/2003Feb/0138.htm...