10 ms·
Every single person writing a REST api should have to memorize this table: GET - Return the current value of an object, is idempotent; PUT - Replace an object
by jedberg 7y ago
Every single person writing a REST api should have to memorize this table:
GET - Return the current value of an object, is idempotent;
PUT - Replace an object, or create a named object, when applicable, is idempotent;
DELETE - Delete an object, is idempotent;
POST - Create a new object based on the data provided, or submit a command, NOT idempotent;
HEAD - Return metadata of an object for a GET response. Resources that support the GET method MAY support the HEAD method as well, is idempotent;
PATCH - Apply a partial update to an object, NOT idempotent;
OPTIONS - Get information about a request, is idempotent.
Most importantly, that PUT is idempotent.
Credit to arkadiytehgraet for retyping the table to be readable. Please give them an upvote for the effort.
- gkoberger 7y agoI agree REST APIs should be built this way, however I disagree any end user should ever have to think about APIs this way. I think a good SDK should hide most of this complexity away, and no real human should ever have to know the difference between a PUT or a POST. APIs should think more like people; people shouldn't have to think more like APIs. EDIT: Since I didn't make my case well enough, here's an example. Let's say you want to charge a credit card. Are you doing a `POST /charge`? Or would it be better to say `card.charge(500)`? My point isn't that there shouldn't be a proper REST API below it, but rather that we should offer abstractions so normal semi-technical people can use APIs and don't have to think too much like a computer.
- mattlutze 7y agoI feel like, if we're not writing data-interfacing applications with PUT and POST as specifically different actions in mind, we're doing something wrong. People think in ambiguity; adding a new item or replacing an existing item, as in this example, shouldn't be something an application needs to guess at.
- jmull 7y agoHow could someone use an API correctly without understanding it?
- alkonaut 7y agoThe client SDK exposes just the actual API and not the web API which is typically much more complex. I really don’t want to worry about setting http headers to make an add-if-not-exists call for example. The API in a client library with a method “TryAdd(...)” is understandable as an API, and the setting of headers and 4XX code you might receive on a duplicate is just returned as false.
- kevin_thibedeau 7y agoA human should know because it affects user agent behavior. Over use of POST where it is not needed causes pointless friction with confirmation dialogs that could be avoided with PUT.
- travisjungroth 7y ago“SDK” is a much less useful hard boundary than “Web API” for where I stop providing service. Should I provide an SDK in every language a client might use? If they want to access my service in a language I don’t support, should I tell them to piss off? I could offer for them to just hit the API themselves, but then we’re back where we started. On the other hand, if I offer a web service and some someone asks “but what if I can’t make HTTP requests?” I’m _perfectly_ happy to tell them to piss off.
- journalctl 7y agoThat’s what Evernote did: binary protocol and implementations in a bunch of languages. Their API was apparently easier to develop this way.
- rswail 7y agoPeople have being using banks for centuries. POST /my-account/charge is not an abstraction, it's pretty much exactly what people do and have done.
- ralmeida 7y agoWhen this happens, it’s usually meant as a RPC endpoint, yes. However, it can be an abstraction, especially if you call it “POST /my-account/charges”, in plural form: you’re sending a new object to the “charges” resource (this architectural technique is usually called reification)
- JMTQp8lwXL 7y agoThere should be an option for deleting many objects. For that, you have to do a POST request on AWS S3: https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteObjects.html https://docs.aws.amazon.com/AmazonS3/latest/API/API_DeleteOb... Instead of polluting existing method names to mean new things, HTTP could offer more method names. Having POST alternatively mean "send a command" makes it meaningless. The command could do anything.
- treve 7y agoWhat advantage is there to having a bulk operation vs many operations? With HTTP2 you can send a ton of requests in parallel.
- JMTQp8lwXL 7y agoA lot of sites might have an HTTP/2 upstream server, but everything downstream is still communicating via HTTP/1.1. I would imagine you can only the benefits of HTTP/2 multiplexing if you fully utilize HTTP/2 end-to-end. That means your CDN, your load balancers, and finally your actual applications-- any any other intermediaries -- all communicating via HTTP/2. Not to say that HTTP/2 isn't the solution, but it's going to be slower than looking up "number of sites using HTTP/2", since you can't easily inspect the behavior of the intermediary servers.
- treve 7y agoGetting onto H2 is worth investing in then though, and drastically simplifies things that would otherwise need bulk operations everywhere. I'm not entirely sure what you mean with downstream, but in my experience H2 is really well supported almost everywhere these days. Ymmv ofc
- JMTQp8lwXL 7y agoWhat I mean is, typically, there are many layers between an end user's network request and reaching the server where it will be fulfilled. For example, you might have your site fronted by Cloudflare. They provide DDOS protection, etc. That Cloudflare server might accept HTTP/2 requests, and then open a new request to your load balancer server. Then, your load balancer opens another network request to the server fulfilling the request. The connections between these servers may only support HTTP/1.1. So you lose the benefits of HTTP/2 since you can't multiplex the network request end-to-end.
- arkadiytehgraet 7y agoEvery single person writing a comment on Hacker News should have to memorize this rule: Never use code formatting for text, as it makes it unreadable on the mobile devices. For my own (and others') convenience, here is unformatted rule table from parent comment: GET - Return the current value of an object, is idempotent; PUT - Replace an object, or create a named object, when applicable, is idempotent; DELETE - Delete an object, is idempotent; POST - Create a new object based on the data provided, or submit a command, NOT idempotent; HEAD - Return metadata of an object for a GET response. Resources that support the GET method MAY support the HEAD method as well, is idempotent; PATCH - Apply a partial update to an object, NOT idempotent; OPTIONS - Get information about a request, is idempotent.
- jedberg 7y agoAs someone who mostly uses mobile, I agree with you, but in this case I couldn't figure out a way to make it readable without code formatting and without retyping it. Thank you for the effort. I've updated my post with your text.
- kevin_thibedeau 7y ago> Never use code formatting for text Until such time that the site supports quoted text a little civil disobedience is warranted. It's not like it's a hard problem.
- PowerfulWizard 7y ago> Until such time that the site supports quoted text a little civil disobedience is warranted. It's not like it's a hard problem. Italics works fine for quote IMO.
- nvrspyx 7y ago____ Italics works fine for quote IMO ____ I propose using four underscores as a delimiter of a quote block, like asciidoctor, in addition to italics. I think it looks better than the markdown way of using ">" in front of a quote as it can get quite cumbersome and ugly when you're quoting multiple lines of something. However, I generally enjoy using markdown more if it works on a website. It just looks ugly IMO if it doesn't.
- fyp 7y agoYou can make anything idempotent if you add a "If-Match" header that returns a 409/412 if the current resource version doesn't match. Not only does this prevent the same request from being applied multiple times, it prevents losing updates when multiple different requests are submitted at the same time. Google cloud storage has a good example of this: https://cloud.google.com/storage/docs/generations-preconditions#_Preconditions https://cloud.google.com/storage/docs/generations-preconditi... The only exception is on resource creation (POST) since there's no existing version/generation number to match on. You can get around that by letting the client generate the id with a random id and reject if the id already exists. I haven't decided if this is a good idea or not since the client can potentially choose really weird vanity ids (especially if you're using uuid in hex or base64). But if you don't expose your internal ids in any UI that seems fine too.
- dgellow 7y agoExpected caching behaviour is as important as idempotency IMHO, and too often ignored.
- rubber_duck 7y agoI wondered recently - where do large queries fit in to this ? URL encoding has size limits and is inefficient - but you can't use get body
- ben_jones 7y agoOr complex nested queries, such as those frequently created in a CRM specifying many key value pairs with different comparators: =, >, <, !=, LIKE. Add complex sorting instructions, pagination instructions, etc.
- sojournerc 7y agoI use POST for complex queries for this reason. Some things aren't easily URI encoded. Easier to just post JSON. Not pure REST, but I'm not really seeking purity...
- rubber_duck 7y agoYea that's what everyone does but it seems like a pretty big oversight in REST
- dragonwriter 7y agoIt's not an oversight in REST, it's one of the ways of trying to faithfully implement REST over HTTP, which has a gaping hole in the standard set of methods that messes with that use case. While HTTP (specifically 1.1) is an implementation of REST principles, that use case wasn't front-and-center at the time (PATCH is also a recent fix to another hole.) There have been proposed standardized fixes such as generalizing the HTTP SEARCH method that first appeared in WebDAV[0], but none have yet gone far. [0] https://tools.ietf.org/html/draft-snell-search-method-01 https://tools.ietf.org/html/draft-snell-search-method-01
- 19870213 7y agoI suppose you could POST the query object and receive a 201 Created with Location header and then retrieve the results via GET?
- chrisseaton 7y agoHow do any of these HTTP verbs interact with HTML? When you have a pure HTML site (no JavaScript) how do you use these HTTP verbs when all you have is <a> links? For example, how do you have an up-vote link? What verb is that supposed to be? And links are all GET aren't they? Up-vote isn't idempotent. How do you make a correct up-vote link? How do we have this disconnect between HTTP verbs and HTML? How did it work before AJAX? (I'm not a web developer.)
- jkeuhlen 7y agoYou can have buttons and forms with pure HTML. Sure links are all GET requests, but you can specify the method of a form.
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- matthewmacleod 7y agoYou’d use forms with buttons in them, and style them to look however you like. I used to do this a lot when building pages using progressive enhancement; it’s less common now. An “upvote” is probably an idempotent action. Calling an “upvote” endpoint moves an object into an “upvoted” state, regardless of what it’s previous state was. However, it’s probably semantically a PATCH, since it’s a partial update of a resource. There is no real disconnect here.
- chrisseaton 7y agoI see things like <a> GET links for destructive operations all the time - are they in violation of the HTTP spec? Do browsers speculatively load these resources and delete things without the user interacting?
- OkGoDoIt 7y ago
- pbreit 7y agoGET and POST seem a ===lot=== easier.
- granshaw 7y agoCareful with PATCH - some enterprises block PATCH requests (had some clients that did this)
- sk5t 7y agoI haven't yet encountered the horrible, deplorable organization that both MITMs its SSL traffic and filters on HTTP method--but I know it's out there, just waiting to ruin my day and diminish my faith in humanity.
- twistedpair 7y agoTrue. Some firewalls will add/remove headers as well. An easy way to lose a day debugging network traces.
- _tkzm 7y agothat is true. people overcomplicate things. but i went the other way, because the number of articles about how people get rest wrong is staggering, so all my api calls are post requests no matther what. what matters is the rpc method being invoked and i truly don't care if someone cannot use plain web browser to perform requests. thats what documentation and api client is for. i have spoken. this is the way.
- rswail 7y agoI'm glad you haven't described your API as RESTful. You're perfectly fine to design an API around RPC calls and POST. There are all of the issues around RPC that you will have to deal with (long running operations, versioning, serde and marshalling of arguments, argument and response typing and interface definitions, codegen of the IDL to product client and server stubs that will require modification and back/forward porting of implementation). But an API definition like this one, covers a lot of that as part of the protocol, most of which is defined by HTTP anyway. You also get encryption, compression, caching, tooling "for free".
- thecleaner 7y agoWhat is the problem that such codes solves ? Why cant it just be json with a POST request with a field specifying the operation ?
- bradleyjg 7y agoYou can always do your own custom thing, but you give up on leveraging anything that anyone has written and anyone trying to implement your thing has to figure out what’s going on from scratch instead of being able to understand it as a variation of something s/he has seen many times before.