A bird's eye view on API development
blog.madewithlove.be
blog.madewithlove.be
There is an HTTP concept created specifically for this idea. Why not use it?
There's no disadvantages to using HTTP verbs over anything else.
> It seems like the author's distinction between POST, PUT and PATCH seems rather arbitrary...
PATCH is… awkward, and probably should be ignored. POST always creates a new resource, under an URL picked by the server. PUT creates or updates a resource under an URL picked by the client.
Can you expand on what you mean by this? In particular, how could HTTP catching mechanism work with POST requests, if the purpose of POST is to change something on the server (so the request should always reach the server)? And in what way is PUT handled differently (by browsers, by proxies, by servers)?
There is probably no single reason why you should never use POST when you need PUT or DELETE. This is mostly an implementation choice, but if you are using a Repository or DAO pattern on the server side, it might be easier to understand if all of the semantics align nicely, e.g. HTTP<POST> -> OOP<Add> -> SQL<CREATE>, HTTP<PUT> -> OOP<Update> -> SQL<UPDATE>.
IMHO using a parameter for method name is a call-by-name strategy that, in this case, is a level of abstraction that just isn't necessary. In other words, there is really nothing dynamic about the behavior. When the application state hits the client side, the flow of control is dictated by earlier events or actions. Your application isn't likely to make decisions on the fly about whether the current flow is concerned with creating an entity versus deleting an entity. For example, the user and the application state "know" already what is valid for an entity and it is likely that your application is reflecting that in a hard-coded "action=[add|update|delete]" parameter. Why funnel this state through a single method with a switch-statement on the server side?
Incidentally, there are some references to caching the results of a POST request[3] but I can't think of an production example that I've run across. It might be cacheable if the response is a redirect to the same "list of entities" URL rather than a direct URL to the newly created entity which is arguably more RESTful but not cacheable.
[1] http://restcookbook.com/HTTP%20Methods/put-vs-post/ [2] https://webmachine.github.io/images/http-headers-status-v3.p... [3] http://programmers.stackexchange.com/questions/114156/why-ar...
POST can be thought of as pasting a file into a folder from your clipboard, without necessarily knowing what its filename is. It oughta tell you the filename after you paste so you can retrieve it later. Typically, in a REST setting, you'd POST a thing to a collection (e.g. POST /api/v1/things)
PUT can be thought of as piping stuff to a file, when you know the filename ahead of time. It completely overwrites any existing data in that file. You'd PUT to the path where the resource should exist (e.g. PUT /api/v1/things/1)
PATCH can be thought of a version control diff. It's actually really powerful, but unfortunately, there's no widespread format to express diffs of deep structural data often seen in REST APIs, so most people avoid it. A PATCH request should contain instructions on how to add and remove sub-fields from a resource. It can be used on collections to batch-add/remove items, and it can be used in single resources to modify them without overwriting other existing fields (or even do all of those with a single request)
I wish it would delve deeper into versioning with some code samples. The way I've done it in the past is just inherit from a controller and just override an action as well as the json-builder. But that because unmaintainable after version 3 or so.
I like how Relay/GraphQL sorta abstracts that away, but Ive had a hard time figuring out how to make that work with the ORM (Active Record) so abandoned for now.
Sidenote: Should credit not be given for the use of the xkcd comic?
That's different to REST's automated discoverability for REST API clients.
On the other hand all the rest of nice bonuses of REST APIs are very useful in such a use case.
I agree with you. The other parts of REST (if you agree that REST can be picked apart and still referred to as RESTful), as surely valuable.
Yes, you can do same with bool flags in response, but if you structure all your decision making like this (around Resources and actions) it's quite easy and natural to make all your components react to un/availability of resource/action, thus accomodating even more complex scenarios - user can list, edit (only some), but can't create new.
Side note: I think there is a typo near the bottom trough should be through in the header based versioning section.