News for hackers. Stuff that matters.
Re:A set of best practices for JavaScript projects by flavio81 on Wednesday July 05, 2017 @11:50AM (#14703397)
> "Only use nouns in your resource URLs, avoid endpoints like /addNewUser or /updateUser . Also avoid sending resource operations as a parameter. Instead explain the functionalities using HTTP methods:

GET Used to retrieve a representation of a resource. POST Used to create new new resources and sub-resources PUT Used to update existing resources PATCH Used to update existing resources. PATCH only updates the fields that were supplied, leaving the others alone DELETE Used to delete existing resources"

This used to be hip and cool and "look ma, i'm modern, i'm doing it REST-style!", but i don't think the fad holds anymore.

In real life, you are going to do much more "operations" to a "resource" than just "retrieve", "create", "update", or "delete", and thus you need to use operation names. It makes more sense to always send the operation name and to standarize on operation names. Also operation names can be more explicit and unambiguous.

It is, for me, a very bad idea to mix up your business model semantics (actions performed on the business model) with the semantics that belong to the OSI-model application layer (HTTP methods: GET, POST, PUT, PATCH...).

Re:A set of best practices for JavaScript projects by richardknop on Wednesday July 05, 2017 @01:10PM (#14704200)
This is also something I think about. It seems that this approach is slowly getting out of fashion and people are starting to prefer APIs similar to SOAP (but JSON based instead of XML).

However if it is a HTTP based API, I'd still think it should be exposed in a way to use HTTP methods correctly. As they align quite well with what web applications will mostly do (CRUD operations).

If you need to do some extra operation, you could create a resource which wraps that operation, for example triggerScheduledJob could be POST /scheduled-job-triggers with body describing parameters to triggerScheduledJob method.

Another approach seems to be to use protocol buffers and something like gRPC. This is good for command line tools or internal communication between services but still, public API exposed over HTTP protocol should probably be designed to accommodate HTTP verbs.

Re:A set of best practices for JavaScript projects by urethrafranklin on Wednesday July 05, 2017 @01:20PM (#14704321)
Actually, I think it is more like simple RPC than SOAP. I'm personally betting big on GraphQL.
Re:A set of best practices for JavaScript projects by HatchedLake721 on Wednesday July 05, 2017 @12:37PM (#14703882)
Do both together.

Use GET/POST/PATCH/DELETE for the simple CRUD on the resource.

Then, put your actions under POST /resource/{id}/actions/{action}, e.g. `/repositories/1/actions/archive`, `/users/1/actions/ban`, `/customers/1/actions/credit-score-with-bank-of-xyz`, etc

Re:A set of best practices for JavaScript projects by urethrafranklin on Wednesday July 05, 2017 @01:18PM (#14704292)
I find the "/actions" slug a bit goofy and unnecessary. Why not just put it at e.g. "/repositories/1/archive"? Additionally, why not maintain restful semantics by using PUT (or more likely, PATCH) instead of POST (which implies creating a new resource)? And "/customers/1/actions/credit-score-with-bank-of-xyz" sounds a lot like a GET request.
Re:A set of best practices for JavaScript projects by HatchedLake721 on Wednesday July 05, 2017 @04:51PM (#14705996)
Because there could be related resources, e.g. "/repositories/1/users", or "/users/1/comments".

I agree it's not necessary, but having /actions just makes it more clear. For example:

"/repositories/1/bans" shows me all bans

"/repositories/1/ban" ban action

I know it's a bad example, but you could end up with other similar related resource and action names that could increase chances of silly mistakes.

Re:A set of best practices for JavaScript projects by denisw on Wednesday July 05, 2017 @02:05PM (#14704771)
I agree about /actions, it is unnecessary. However, for these kinds of "action resources", POST is definitely the best-matching request method. It does not mean "create a resource" (although this is one case it is often used for), but "process this request in your custom resource-specific way". The HTTP RFC formulates it like this [1]:

> The POST method requests that the target resource process the representation enclosed in the request according to the resource's own specific semantics.

Given that what an action resource does is, by definition, resource-specific, POST fits perfectly.

[1]: https://tools.ietf.org/html/rfc7231#section-4.3.3

Re:A set of best practices for JavaScript projects by always_good on Wednesday July 05, 2017 @01:09PM (#14704196)
I don't see why you'd want the /actions/ segment at all.

That's like using a /resources/ prefix at the start of every endpoint.

Re:A set of best practices for JavaScript projects by partycoder on Wednesday July 05, 2017 @11:53AM (#14703434)
(Removed this comment as it was incorrect)
Re:A set of best practices for JavaScript projects by yes_or_gnome on Wednesday July 05, 2017 @12:01PM (#14703530)
No. HEAD is the same as GET, but the server should not return a response body. The response code and headers should all be the same. This is to guarantee that a path is serviceable, response body size, and for verifying the client's cache.
Re:A set of best practices for JavaScript projects by partycoder on Wednesday July 05, 2017 @12:08PM (#14703605)
You are correct, sorry about that. Removed my comment to not spread incorrect information.