DEV Community

Ditch REST, go intent based

Christopher Ribeiro on January 05, 2024

At AlertPix we allow streamers to receive donations from their audience via Pix Instant Payment and show an alert on the live stream. Our API excl...
Collapse
 
auroratide profile image
Timothy Foster

The typical approach for your example is to use query parameters.

GET /books?author=:id
Enter fullscreen mode Exit fullscreen mode

This allows us to query by multiple things if necessary.

GET /books?author=:id&genre=mystery
Enter fullscreen mode Exit fullscreen mode

If you use the path for that, you run into the same which-way-around issue we were trying to avoid.

GET /books/by-author/:id/by-genre/mystery
...or...
GET /books/by-genre/mystery/by-author/:id
Enter fullscreen mode Exit fullscreen mode
Collapse
 
slobodan4nista profile image
Slobi

Classic Rest is pretty common wel built and well adopted.
This is just adding to confusion by doing things in a unique way. Rearly a good idea doing any API not just web.
Respect for trying to move things in some direction, but this seems not to be it.
If this is just a contraversial post for attention, good job, got me 👍

Collapse
 
610470416 profile image
NotFound404

you may need to know the difference between rest apis and vig apis.
where rest apis are for resources and vig apis are for logics.
And with the backend js web framework aex you can combine them together into one class.
github.com/calidion/aex/blob/maste...

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

I feel compelled to pile up on the negative to accept this acceptable. I'll start by saying that user interfaces are for humans, while API's are for machines: The uer interface is run by a machine, and this machine uses the API to communicate with the back-end. See? Machine to machine. The human uses the UI.

Timothy Foster is right: When you are querying, usually you query with the query string part of the URL. The added path segments are done for a different purpose. Examples:

Get all books from an author: /authors/:authorId/books

These are by no means mandatory, as this can be expressed as /books?authorId=:authorId. I personally provide the former as I believe is quite expressive and unambiguous.

Collapse
 
ranjancse profile image
Ranjan Dailata • Edited

Sorry, one of the deadliest misconceptions of Restful Services is to think in terms of HTTP. But that's not the right thing. Understanding the basics of resources, sub-resources, etc. is the key thing for the success. Hence, I would highly recommend everyone to invest on the basics, understand the intent and motivation behind the creation of the most patterns and best practices.

Collapse
 
m3rashid profile image
MD Rashid Hussain

Totally OK for RPC style APIs. If you don't provide your APIs for others to use, it's ok. REST is more of a standard (well known/good practice) rather than a principle