My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

What is the best practice to write custom RESTful API endpoints?

Ehsan Fazeli's photo
Ehsan Fazeli
·Feb 13, 2019

Suppose we have an entity named Comments. And there's regular CRUD for interacting with API requests through below URLs:

  • GET : /:postId/comments
  • POST /:postId/comments
  • GET /:postId/comments/:commentId
  • PUT /:postId/comments/:commentId
  • DELETE /:postId/comments/:commentId

My problem begins when we need to add a new endpoint to either reject or accept by some administrator working with the application.

Here are some available scenarios to achieve this functionality:

  1. Define a new route with a PUT method and /moderate URL
  2. Define two new routes with PUT methods and /accept or /reject URL
  3. Define a whole new resource to moderate the comments
  4. Send some extra info through the existing /:postId/comments/:commentId URL to tell the controller we're moderating

What's the best practice to develop this kind of endpoints?