Ok, here is the fact. StackOverflow is implemented REST-style. When you visit a specific questions/$id/ URL you get to see the question. The content is returned in HTML because it's what the browser understands.
I have to develop my own REST service. Fact is that I have to return multiple formats for the same information. For example, the default could be HTML, but I could return also an XML or a JSON.
Question is: what is the recommended style to achieve this ? Three choices (more from your helpful suggestions)
Same is for PUT (POST) operations. If I want to submit data in different formats, I need to inform the receiver the format I am providing, so the same situation (and question) holds.
Thanks!
Edit: Additional proposal is the following
4) Specify a proper URL for each format e.g. http://example.com/questions/12345.json . This looks nice, but wouldn't this mean that, for consistency, we should also have http://example.com/questions/12345.html ? sounds so 1995... :)
PS: I hate markdown putting an arbitrary order to the list. If I want to start with 4, I should be able to do it.
I recommend that you support more than one format - that you push things out in one format and accept as many formats as necessary. You can usually automate the mapping from format to format. Digg allows you to specify in two ways: in a pure RESTful way in the Accept header or in the type parameter in the URL.
The REST API supports the following data formats: application/json. application/json indicates JavaScript Object Notation (JSON) and is used for most of the resources. application/xml indicates eXtensible Markup Language (XML) and is used for selected resources.
The currently-available response formats for all REST endpoints are string-based formats and include XML, JSON, PJSON, and HTML.
Direct data formats are best used when additional APIs or services require a data stream from your API in order to function. The three most common formats in this category are JSON, XML, and YAML.
Option #3, setting the HTTP "Accept" header, is more in keeping with the HTTP specification and, among REST purists, is considered most correct. It also means that you keep the same URL (resource) no matter what the representation is.
You may, however, encounter user agents that aren't capable of setting an Accept header, so supporting a backup mechanism for specifying the response format is advisable. In that case, I suggest a URL query string parameter. Using a URL query string parameter means you keep the same core URL, no matter the content type returned. This makes it clearer that clients should only issue a PUT to that URL and not to the /foo/bar.json or /foo/bar.xml URLs.
Edit: Another thing to consider if you decide to go with the URL suffix (i.e. foo.json vs. foo?format=json) is that you may run into issues with caching proxies. If someone issues a PUT to /foo.json, a proxy won't interpret that as a request to invalidate /foo.xml. If, however, you use /foo?format=json, then it's all stored under the same resource (/foo) in the proxy.
I would go for Option 1 (URL parameter) as it's the most consistent with the principles of REST, and the most pragmatic.
Option 2 smells bad - you're talking about different representations of the same resource, so you should use the same URI for them.
Option 3 smacks of being too hard to control - how would you hand-test it from within your browser, for instance?
(The same arguments apply for PUT
/ POST
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With