3 ms·
> As the article points out, there are all too many HTTP libraries that only support GET and POST, not the more "esoteric" verbs like PUT and DELETE. GET and P
by naasking 10y ago
> As the article points out, there are all too many HTTP libraries that only support GET and POST, not the more "esoteric" verbs like PUT and DELETE.
GET and POST are all you really need for REST.
- int_19h 10y agoWhat we really need is a set of verbs that allow to reliably distinguish between these three types of operations: 1. Pure read. 2. Impure (stateful) read. 3. Idempotent write. 4. Any other write. There's no particular reason to separate inserts, updates, deletes etc as part of the protocol - they're all just different kinds of writes, and middleware doesn't derive any benefit from being able to distinguish them. Thus, this can be a part of the payload. On the other hand, the difference between reads and writes, and the two subdivisions within each, do matter for purposes such as caching and automated error recovery (e.g. a proxy can repeat an idempotent write a few times before returning the error to the originator of the request). In REST, we have GET for #1 and #2, PUT and DELETE for #3, and POST for #4. In practice, this is often simplified to just POST used for both #3 and #4, but that loses a valuable distinction (but is unfortunately often necessary because of the lack of support for other methods). On the other hand, the PUT/DELETE distinction is largely pointless.
- edoceo 10y agoPATCH verb? I've seen it in the wild
- int_19h 10y agoIndeed, I forgot some. Then again, last time I tried to use PATCH, I found that support for it in various places in the stack was patchy enough to make it a non-starter.
- naasking 10y agoIt's not worth making the distinction between idempotent and non-idempotent writes given the possibility of network partitions. Every write must be made idempotent because all you can do is retry in this case. PUT is merely an optimization on idempotent POST requests, one that will become progressively rarer as HTTPS continues to spread. POST requests can be made idempotent by binding the result of processing that request to a future, so every subsequent request simply returns the already computed result. Impure reads are the default, and pure reads are designated by long-lived cache headers, ie. it's an impure read that lasts as long as the server responds. So really you just need GET and POST, and server-side frameworks should make POST requests idempotent by embedding some notion of futures for side-effecting operations. See the Waterken server for the first development platform to really get this right.
- int_19h 10y agoAn insert cannot be idempotent by the very definition of the operation.
- joncrocks 10y agoIt can be as long as you have some kind of identity information associated with the request, and provided alongside any requests that are the 'same'. e.g. Instead of saying "Insert a transaction for £20", you say, "Insert a transaction that entity A calls 1234, for £20". Stripe use something like this to ensure that as long as you call their API to ask for a payment and use the same key, they won't charge the person twice. https://stripe.com/docs/api#idempotent_requests https://stripe.com/docs/api#idempotent_requests
- naasking 10y agoRequesting an insert can be idempotent. Each non-idempotent operation is tagged with a unique identifier, if the server doesn't have inserted data tagged under that identifier, then it performs the insert, if it does, then it returns the usual success code as if it had just performed the insert. This unique identifier is simply a durable representation of a future which I described above.
- int_19h 10y agoThis would seem to require the identifiers to be attached to records in perpetuity (i.e. it would basically require client-generated IDs everywhere), so that the server can reliably verify that this operation has already produced a record. I can see it working, but it's a far-reaching change, that may not be easy to adopt for existing data storage schemas.
- naasking 10y ago> This would seem to require the identifiers to be attached to records in perpetuity (i.e. it would basically require client-generated IDs everywhere) Not sure what you mean by client-generated. The server-side app generates the id because it has to store and interpret it. This can be as simple as a sequence number, similar to how TCP guarantees delivery. It depends on the schema really. This is the simplest way to ensure you can perform arbitrary retries of POST in case of a network partition. This data doesn't even need to be integrated with your app's schema, although that's ideal so you can manage the storage lifetime. But you could use an entirely separate store for GUIDs and cached replies, and so it becomes transparent to your app and just becomes another layer.
- richardwhiuk 10y agoWhy oh why oh why do you want a stateful read? That will be the beginning of the end for your architecture.
- tdriggs 10y agoLogging/analytics? Paywalls?
- int_19h 10y agoBecause applications have state. In fact, the vast majority of reads in any applications are stateful (the database backing your app is also state, you know; I'm not talking just about session state here). By "pure" here I mean that we're talking about a pure function of its inputs.
- icebraining 10y agoWhy do you need to distinguish between those? In fact, if the function is pure, why not run it on the client?
- int_19h 10y agoBecause the server has more computational resources, for example. It's a very rare scenario in general, but it does happen. Why distinguish? Because if you know that the results are not affected by server state, they can be cached much more aggressively.
- jandrese 10y agoYou mean like a cursor in a database?
- nippoo 10y agoRate limiting APIs?