Hacker News new | ask | show | jobs
by JoshTriplett 949 days ago
Other than for images and video, when in practice do you want to do that?

If you're talking to an API, you should know what that API can produce.

And if you're talking to a webpage, this isn't an issue.

2 comments

> when in practice do you want to do that?

Well as you're quite interested in APIs: the accept header is a fantastic way to version a REST API, and even has a built in way to handle clients that can talk to multiple versions of the same API.

Your app could request just 'application/vnd.foo.v1+json' from an API and if it's a centralised service that may be fine.

If the API your app talks to is something that's deployed to customers, or rolls out to regions incrementally or whatever, and thus can be at different versions, you need a way to handle that: the Accept header has you covered.

Everyone does this by putting /v1/ in the api url. It's massively more visible, gets logged properly, and isn't annoying to request from tools like curl.

The Accept header does nothing that URLs can not.

> Everyone does this by putting /v1/ in the api url

URLs are opaque identifiers that only have meaning to the server that generated them. This makes any meaning implicit, where the accept header is an explicit and documented part of the interface. Maybe not much difference in practice for code you've interacted with, but that doesn't mean no difference at all.

If curl has trouble setting headers, that's a curl problem.

You saying "no one needs that" doesn't mean it doesn't do more than some arbitrary url parameter or path segment. It means you're choosing not to use it.
If you can choose to not use it, then you don't need it. It doesn't enable any new capabilities.
An API can return JSON, XML, HTML microformats, maybe even a binary encoding of some kind, possibly more down the road. All of these are serialized formats for objects.

Of course you can have a unique URL for each format and avoid the accept header, just like you don't technically need more than GET and POST methods.