5 ms·
> You are mutating state with HTTP GET I agree. However, this is the first API I've seen where a mutable GET actually makes sense. Drawing a card is definitely
by Kiro 12y ago
> You are mutating state with HTTP GET
I agree. However, this is the first API I've seen where a mutable GET actually makes sense. Drawing a card is definitely a GET. How would you do it instead?
- anilgulecha 12y agoA PUT would work, with action=draw&count=2, or action=shuffle, and so forth.
- kikibobo69 12y agoPUT should be idempotent, though.
- bdcravens 12y agoCould each draw return a draw_hash, that is usable only once? Not only could you then have idempotent draws, you could easily embed it as hypermedia,a previous hash could be sent to a hand_history to return an array of point in time data
- nl 12y agoAs a general rule "action" parameter is pretty non-RESTful. Sometimes RPC-style interfaces like that are ok, but don't pretend that they are RESTful. Also PUTs are supposed to be idempotent, so don't use a PUT for this.
- dghf 12y agoI thought URLs (including query strings) were opaque under REST?
- vog 12y agoURLs are opaque, but the meanings of the HTTP methods aren't.
- dghf 12y agoI'm not questioning that; I was questioning the statement '"action" parameter is pretty non-RESTful'. If URLs are opaque, why is it non-RESTful? Or is the point that if you're using query-string parameters, then by definition you're using a method that's supposed to be safe or idempotent?
- vog 12y agoIf you used different HTTP methods, you'd already know which one to use, and don't need the action parameter. So the usage of the "action" parameter usually implies that that requests are all the same method. And if we don't want to violate the HTTP standard, that method must be neither GET/HEAD, nor PUT nor DELETE (as surely some actions are non-idempotent). In other words, an action parameter which is actually used and which doesn't violate the HTTP standard, well, that almost certainly means that all requests are POST requests. However, if all requests are POST, no matter if they are side-effect-free or not, no matter if they are idempotent or not, then this is not very REST-like. Note that it is not important whether you think this argument does or doesn't holds for your particular API. My point is that this whole judgement is solely about HTTP methods, and has nothing to do with URLs being opaque.
- dghf 12y ago> If you used different HTTP methods, you'd already know which one to use, and don't need the action parameter. But the examples given by anilgulechas were 'action=draw' and 'action=shuffle'. Neither are idempotent, let alone safe, so presumably the only method to use in either case would be POST (notwithstanding anilgulecha's suggestion of a PUT). So we need to distinguish between POSTs requesting a draw, and POSTs requesting a shuffle. Two options: a) indicate it in the POST's body b) indicate it in the URL. If we go for (b), the query string seems as good a place as any. What have I got wrong? Where would this violate REST (or the HTTP spec)?
- xnxn 12y ago> Drawing a card is definitely a GET. I see where you're coming from ("I'm drawing / taking / GETting some cards"), but drawing cards is not an idempotent action. I would implement it as a POST.
- ryan-allen 12y agoI think it's more of a terminology thing, rather than 'GET me a card' you could 'POST remove 2 cards from the deck' and the side effect is the response tells you which cards were deleted. But this kind of bike shedding drives me mad because oh my lord who gives a crap just build software and document it properly.
- wetmore 12y agoIt's not necessarily bike shedding though. GET and POST have two different, widely-agreed upon semantic meanings. It's not just terminology.
- Confusion 12y agoI give a crap, because when I see an API, I expect a GET to be idempotent. If a coworker implemented this, there would be words.
- whyaduck 12y agoSame here. Working on a large codebase where people "just build software and document it properly" without following standards is not fun. Not to mention that frequently people end up shortening that to "just build software". It's better to get in the habit of following good standards even on small projects before bad habits form.
- Confusion 12y agoIn some sense you are PATCHing the deck by removing a card.
- hartror 12y ago
- crimsonalucard 12y agoWhy conform to arbitrary conventions when they don't make sense? Personally I think the best reason to conform to REST is because the user expects a "good" api to conform. Other then that is there any hard reason as to why it's "better" to strictly follow REST or even follow it at all? Is an api that is consistent, fast and documented well, but uses only GET requests for everything including state changes really a horrible api?
- vog 12y ago> Is an api that [...] uses only GET requests for [...] state changes really a horrible api? Yes, it is. Because HTTP clients (including browser) rely on the common method properties as defined by the HTTP standard (GET having no side effects, PUT being idempotent, etc.). The simplest example is caching / proxying, but there is other behaviour. I strong recommend acutally taking a look at the HTTP standard. The RFCs such as RFC 7231 are well organized and human readable. For the definition of the HTTP verbs, see: RFC 7231 Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content 4.2 Common Method Properties https://tools.ietf.org/html/rfc7231#section-4.2 https://tools.ietf.org/html/rfc7231#section-4.2
- alexhill 12y agoI agree with the spirit of your post, but using the right HTTP verbs can have real, actual consequences because of how some caches and clients behave. You're more likely to accidentally send multiple requests or get stale data when you're using GET.
- Drakim 12y agoI would argue that "drawing a card" isn't merely an information look up (GET) but an action with consequences (POST). Looking at the cards in your hands, if the server maintains your hand state, would be a GET though.
- hartror 12y agoAs others have pointed out the HTTP verbs have meaning at a protocol level, trying to match a verb based on its English meaning is fraught with problems. Not least of which is the proxy problem I pointed out in my original comment.
- jalfresi 12y agoDELETE a card from the deck. Return the card(s) drawn in the response body. Have a separate resource for the top of the deck: DELETE /api/<deck-id>/top?count=2 ------ 200 Ok 8♣ 10♥
- hartror 12y agoDELETE is idempotent to quote a Stack Overflow answer on the subject: > Idempotence refers to the state of the system after the request has completed > ... > The key bit there is the side-effects of N > 0 identical requests is the same as for a single request. http://stackoverflow.com/questions/4088350/is-rest-delete-really-idempotent http://stackoverflow.com/questions/4088350/is-rest-delete-re...
- jalfresi 12y agoThe example I gave is idempotent. Calling it n >0 times would result in the same side effects.
- thedufer 12y agoIt is acceptable for a client to retry a request that failed if it is a GET. This could happen if the server got your request but the response got lost, in which case you can't count on the deck having the right number of cards.