7 ms·
This says versioning in the URI is wrong. Why? I much prefer separating my API versions into different files at the dispatcher level. Doing it with the header w
by StavrosK 15y ago
This says versioning in the URI is wrong. Why? I much prefer separating my API versions into different files at the dispatcher level. Doing it with the header would be a mess in most frameworks, and I don't see what it hurts.
Also, what about custom HTTP verbs? Many times I need something more than GET/PUT/POST/DELETE. What happens then? I haven't seen anyone talk about that.
- decadentcactus 15y agoWhat I've come to realise is that if you try to make an API of any sort, somebody, somewhere, will write a post about how you're defiling the HTTP protocol.
- sirclueless 15y agoTotally true. For example, this author would have problem's with reddit's API, where you indicate that you are requesting JSON by adding .json to any URL. And you know what? The world doesn't come crashing down.
- almost 15y agoThere's nothing wrong with doing that. A resource can have multiple URLs and multiple representations. That's fine, in the right circumstances.
- sopooneo 15y agoYou say that, but a lot of people scream bloody murder about it. And this exemplifies one of the major problems for people like me that are trying to learn how to create REST APIs and understand why to bother doing so in the first place. The problem is that even people who seem to know what they are talking about fundamentally disagree on many things. And Fielding's dissertation does not cover many implementation details. It's like interpreting the constitution trying to figure out how it should all happen in the real world. Some of the points of contention: (1) Should api version go in the URL or accept header? (2) Should representation type go in the url or accept header? (3) Are custom HTTP verbs okay? (4) Should GET, POST, PUT and DELETE map one-to-one to CRUD? Most say no, but other seeming authorities say it's fine. (5) Should you ever return a URL template, and a set of entity identifiers that can be plugged in, or should you only return the fully formed URLs? (6) What is the difference between a URL and a URI? (And all you who say this one is dead simple and I'm an idiot for not knowing, see if your answers actually agree with each other) (7) Are two different language translations of a document different resources, or are they different representations of the same resource? (8) If two different language translations are just different representations of the same resource, how should the client indicate which version it wants? And of course some people say that a few of these questions aren't even within the scope of REST anyway.
- almost 15y agoSome answers, may be wrong but this is how it makes sense to me: 1. Possibly both (the one in the URL being the version of the URL-space and the one in the Header being the more fine grained Representation Version). 2. It depends. For simplicity it often makes sense to put it in the URL. For more generality headers can be better. 3. No 4. They don't have to. They usually do though and it makes most sense when they do. 5. Fully formed URLs are simpler, so go with those if you can. But sometimes you'll need more. 6. URLs are a subset of URIs. All URLs are URIs. URLs give the location of the resource whereas URIs just have to identify it (but could also give the location of course. 7. It depends. I lean towards different but I'm no authority. 8. Using the Accept-Language header. But I'm guessing this is not always the best way to do things.
- almost 15y agoWith a RESTful API you don't tend to need IDs most of the time, you just use URLs. So having all the versioning info in the URL is not so great, you change the version and suddenly all the URLs aren't valid anymore. But more important that is upgrading a version on an API may not be an all or nothing thing. You might want to start using the new features of the API on one resource type but you aren't ready to upgrade your usage on everything else. If the version is in the API you'll have to take apart and put back together urls to get the right ones, this is logic you don't want to have to encode into your client. If on the other hand you use Accept headers to do versioning you can have as fine grained control as you need. Regarding the custom HTTP verbs it may seem like you need those at first but in practice you really don't and there is almost always a good clean way of doing things that doesn't break anything (or so I've found). I find that the solution is usually to introduce another resource or two, the transactions example from the article is a perfect example of this. I know it sometimes seems like this is all abstract stuff that has no impact on the real world but it does make sense eventually! It's really lovely to use a properly RESTful API :)
- nirvdrum 15y agoHuh? The whole point of having a version in the URL is so you can roll out a new set of URLs and their corresponding endpoints without touching the old ones. When v2 of the API comes out, you still support v1. That's no different than using headers. If you sunset or change an API call, the header isn't going to save you any trouble. In fact, since the client now won't get a 404, but rather something else, the cost of debugging goes up pretty substantially.
- almost 15y agoBut how do you deal with the partial upgrade scenario? I might want to use a few of the new features in my client code but not have the time to upgrade (and test!) everything just yet. Of course version numbers in URLs can have their place too. But I think they should only really be used in situations where you have upgraded so much that the original URL space just makes no sense. And if you can avoid huge breaking upgrades like that through good initial design then all the better :)
- thomasbachem 15y agoYou're using the wrong frameworks :)
- scott_w 15y agoCustom verbs aren't really necessary. You can do some of your "method" naming in the URI. One example could be: /sum?number=1&number=2 which, when a GET is issued, will return a representation of 3. If you need to perform some action that can be expected to take time, you can issue a POST request, which can give you a new resource that reports back on its current state. An example could be: POST /printer with the request body containing the document to print. It could assign a URI that tells you the status of the document (location in queue) and you can DELETE it if you no longer want to print the document.
- brown9-2 15y agoIt sounds like you are making an API design choice based on how easy it is for you to implement something internally. What about your client's ease? Version in the URL requires them to update the URLs everywhere. Version in the accept header is likely a one line change somewhere.
- digitaltoad 15y agoThis is not necessarily true. A client could easily have the api version segment of the url in a central configuration location.
- javascriptlol 15y agoThe problem is designing all this nonsense into the bottom layer in the first place. I've never heard a good argument as to why any of this is better than just starting with raw sockets for an app talking to a server. Take HATEOAS for example; a completely academic waste of time. Writing a tiny bit of API documentation isn't hard. Caching is an awful thing to be designing in without knowing the specific application. Verbs are a hack bolted on to make hideous form/link driven applications work when the user reloads or moves back/forward.
- javascriptlol 15y agoDon't downvote, comment. Or admit to yourself that you can't respond to my criticism.
- javascriptlol 15y agoWow, what a weak crowd. No substantive criticism, just downvotes. The HN crowd should please refrain from arguing on the Internet about design until they've learned some basic systems programming.
- javascriptlol 15y agoPlease downvote this comment too. The more you downvote me without commenting, the more you confirm my superior skills in system design.
- psadauskas 15y agoExcept that when you follow all of REST, including HATEOAS, that part of the API documentation is FREE. And the caching is FREE, because its already been designed for you. Which is the whole point.
- javascriptlol 15y agoWhat? How can you say REST is going to give you a bunch of stuff "for free" when you haven't even seen the application. How do you know you're even going to get what you want? This is the central conceit here.