News for hackers. Stuff that matters.
Re:So you want to offer a public API … by transmit101 on Wednesday July 25, 2012 @07:48AM (#4290408)
Please, please don't help to persist the convention of putting the API version number in the URL. There are far better ways of doing it.

http://stackoverflow.com/a/975394/62871

Re:So you want to offer a public API … by lucaspiller on Wednesday July 25, 2012 @08:01AM (#4290447)
I'm not convinced that versioning media-types is the best way. I agree it is a more correct way as opposed to basically abusing REST, but I think overall putting the version in the URL is better.

From the linked answer:

> * you break permalinks

> * The url changes will spread like a disease through your interface. What do you do with representations that have not changed but point to the representation that has? If you change the url, you break old clients. If you leave the url, your new clients may not work.

Putting the version somewhere else in the request doesn't fix this. If you drop support for an old API version it is going to break stuff. It is easier to spot the issue if you return a 404 rather than a 500 or 400 error, or even correct data that breaks the app consuming the API as it is expecting something else.

> * Versioning media types is a much more flexible solution.

The main issue I have with this is I have had the "pleasure" of working with pretty stupid people implementing clients on my API. They easily get POST and GET requests mixed up, HTTP and HTTPS, whether or not the request should be authenticated. I don't want to add something else to confuse them even more...

Re:So you want to offer a public API … by rapala on Wednesday July 25, 2012 @08:22AM (#4290520)
> The main issue I have with this is I have had the "pleasure" of working with pretty stupid people implementing clients on my API. They easily get POST and GET requests mixed up, HTTP and HTTPS, whether or not the request should be authenticated. I don't want to add something else to confuse them even more...

So you are willing to do the work of those other devs for them, and cripple your own API in the process?

Re:So you want to offer a public API … by andrewflnr on Wednesday July 25, 2012 @09:45AM (#4290876)
If he wants to expand his user base, yes he is. The essence of a business is doing other people's work for them and then charging them. If doing other people's work for them also brings you money, well, you'd better have a very good reason not to.
Re:So you want to offer a public API … by MatthewPhillips on Wednesday July 25, 2012 @08:11AM (#4290484)
> The main issue I have with this is I have had the "pleasure" of working with pretty stupid people implementing clients on my API. They easily get POST and GET requests mixed up, HTTP and HTTPS, whether or not the request should be authenticated. I don't want to add something else to confuse them even more...

Where does the madness end? Do you treat everything as though it were GET and stick the real method (and every other request header) in a query parameter? I think it's ok to assume a basic level of understanding of one of the best documented specs in computing.

Re:So you want to offer a public API … by mattlong on Wednesday July 25, 2012 @01:46PM (#4292070)
What's your definition of "basic level of understanding"?

A public API is about 99% of what we (Crocodoc) offer. While designing the latest version of our API, I always erred on the side of staying true to REST principles. Fortunately there were others on our team that erred on the other side. The result is a happy medium that certainly veers from the canonical definition of REST but is arguably easier for Joe Developer to play with. Guess what...we still receive an incredible amount of support requests from developers getting stuck.

It's tempting (and fun) to build APIs "the right way" and use little tricks like putting versioning in the media type. However, that's pretty often at odds with product/user experience/business considerations. Why offer a product that only 10% or 20% of your customers are savvy enough to use?

Re:So you want to offer a public API … by lucaspiller on Wednesday July 25, 2012 @08:23AM (#4290528)
Each to their own I guess. However personally I would rather not spend time tracking down a "bug" due to a client not consuming my API as documented. If I can make it easier for them to use, by not doing things the "correct" way, then I would rather do that.
Re:So you want to offer a public API … by j_s on Wednesday July 25, 2012 @02:09PM (#4292185)
This is often what the pragmatic/practical approach prioritizes (easier for users), as opposed to the idealistic/academic.
Re:So you want to offer a public API … by andraz on Wednesday July 25, 2012 @08:03AM (#4290455)
exactly! All use of headers in web APIs just makes it harder to understand them by regular developers of API cleints.
Re:So you want to offer a public API … by Graphon on Wednesday July 25, 2012 @02:36PM (#4292327)
Using a content-type header instead of .json or .xml (etc) extension in the URL is just another example of this phenomenon. There's a reason people are moving to use dot extensions in URLs - it's easier to adopt and test.
Re:So you want to offer a public API … by MatthewPhillips on Wednesday July 25, 2012 @07:59AM (#4290437)
Also don't put the format in the URL, that's what Accepts is for.
Re:So you want to offer a public API … by andraz on Wednesday July 25, 2012 @08:01AM (#4290445)
While in theory great Accepts are just additional burden for web developers. We don't want to need to deal with headers. We want to fetch an url and get _the_same_data_ no matter if the url was entered in the browser, wget, or inside the app we're writing.

For a simple developer, output format is just another parameter and it shouldn't be hidden in HTTP stack.

Re:So you want to offer a public API … by MatthewPhillips on Wednesday July 25, 2012 @08:07AM (#4290467)
I don't think 1 additional line of code is much of a burden. If you put it in the URL you're forcing the server to ignore the Accept header that the browser sends and pay attention to some non-standard url substring instead.
Re:So you want to offer a public API … by Gigablah on Wednesday July 25, 2012 @08:41AM (#4290598)
I was fooling around with Javascript MVC lately, and came across a problem -- let's say I have this URL, "example.com/objects/1". If I open it in the browser, the server returns a barebones HTML page. JS in the page makes a separate ajax call to the same address, the server detects the accept header, returns JSON data, and the page is populated with content.

Now I navigate off the page to a different site, and press the back button. Instead of the HTML page, I get the cached JSON response. Now if I change the ajax call to "example.com/objects/1.json" instead, that keeps the URLs separate and the browser won't cache the wrong response. Is there a better way to solve this?

Re:So you want to offer a public API … by MatthewPhillips on Wednesday July 25, 2012 @08:58AM (#4290674)
The server needs to respond with a Vary header which tells the user agent (the browser) how to identify what it should cache.

[1]https://developer.mozilla.org/en/HTTP/Content_negotiation

Re:So you want to offer a public API … by e98cuenc on Wednesday July 25, 2012 @12:39PM (#4291722)
The problem I have with Vary headers is that many reverse proxies will just not cache anything with a Vary header. The reason is that if they add the value of the header that "varies" to the key of the cache, now they have to cache under many different keys the same content.

Think of all the different "Accept" headers that clients can send you, each one of these will get an entry in your cache. And now add other headers that should also be in "Vary" like "Cookies", "Accept-Language", etc. and your cache will be virtually useless.

Re:So you want to offer a public API … by buddydvd on Wednesday July 25, 2012 @03:31PM (#4292601)
Interesting. Perhaps, instead of relying on "Vary: Accept" the server should respond with: "Vary: YourCustomHeader". The API client, on the other hand, will include "YourCustomHeader: SomeConstantValue" when making requests to the server. That way, the reverse proxy will only store up to two versions in its cache.
Re:So you want to offer a public API … by Gigablah on Thursday July 26, 2012 @09:17AM (#4295800)
Unfortunately, Chrome disregards the Vary header :(

http://code.google.com/p/chromium/issues/detail?id=94369

Re:So you want to offer a public API … by Gigablah on Wednesday July 25, 2012 @10:26AM (#4291067)
Ah, so I should set "Vary: Accept". Thanks, that was a great help.
Re:So you want to offer a public API … by andraz on Wednesday July 25, 2012 @08:12AM (#4290487)
It's not just one additional line of code. It is additional work every time you try to view result in the browser. Which you basically can't because you can't put accept header in url bar..

At least in python, making requests becomes quite a few more lines of code if you want to add headers. There's usually a short-hand command if you just want to get full data from an url and a "create-an-object, then set these parameters, then this, then open a connection then read what's there".

Mandatory headers are just a way to make life of a regular developer painful.

Re:So you want to offer a public API … by MatthewPhillips on Wednesday July 25, 2012 @08:23AM (#4290527)
Sounds like you just need to abstract that part into a function. Much better solution than breaking HTTP. HTTP is not supposed to send the same results for a given URL, it is supposed to send the same results for the a given request. In the case of the browser, it asks for HTML.
Re:So you want to offer a public API … by qw on Wednesday July 25, 2012 @12:33PM (#4291692)
I can see that using "Accept" can be considered a more elegant solution, I don't see how adding a parameter will break HTTP. There is nothing in the HTTP standard that says what an URL should or should not return. The Accept-header is just an additional source of information.
Re:So you want to offer a public API … by MatthewPhillips on Wednesday July 25, 2012 @03:15PM (#4292503)
Not actually, the Accept header isn't a mere suggestion. If the server chooses not to fulfill it, the correct response is 406 Not Acceptable. This is why browsers send something like this in their Accept header:

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

This says "we would prefer one of these formats, but we'll take whatever you got".

Re:So you want to offer a public API … by rapala on Wednesday July 25, 2012 @08:34AM (#4290568)
This is exactly the reason why even the good standards fail. Instead of writing 10 lines of code to abstract your specific use case, you want it to be the default behavior. How many times have you cursed something like IE for not implementing CSS properly?
Re:So you want to offer a public API … by icebraining on Wednesday July 25, 2012 @08:36AM (#4290577)
Modify Headers[1] lets you easily configure the headers your browser sends: https://addons.mozilla.org/en-US/firefox/addon/modify-header...
Re:So you want to offer a public API … by mikegirouard on Wednesday July 25, 2012 @08:20AM (#4290514)
Excellent point. I can't agree more. In the long run, this makes things far more flexible.

Also, I might be nitpicking here, but I think the difference between a URL and a URI is that the URL explicitly defines the media type.

If that's the case, then the advice would be to use URIs pointing to resources in combination with `Accept` headers.

Re:So you want to offer a public API … by icebraining on Wednesday July 25, 2012 @08:38AM (#4290583)
Also, I might be nitpicking here, but I think the difference between a URL and a URI is that the URL explicitly defines the media type.

No, no. An URL is an URI. And the URL doesn't define any media-type, those are hacks created frameworks; there's no such thing in the spec.

Re:So you want to offer a public API … by j_s on Wednesday July 25, 2012 @02:20PM (#4292244)
I think the server-side could easily support both, to keep the purists happy and still be easy to use/work through proxies/caches/etc.
Re:So you want to offer a public API … by anderse on Wednesday July 25, 2012 @08:45AM (#4290613)
Beware that proxies sometimes strip the accept header. I know of at least one European ISP that does this for its 3gs users.