REST api : correctly ask for an action

130 views Asked by At

I'm currently working on a REST api. I've read a few times how to handle endpoints the right way, using the protocol (post, put, ...) to define which action should be made.

Let's say I have a list of quotes. I have :

  • a GET endpoint /quotes that let me get all my quotes
  • a POST /quote to post a new quote
  • a GET /quotes/ID to get a single quote
  • a PUT /quote/ID to update a quote.

Now I want to add the ability to :

  • share a quote with another member
  • mark a quote as favorite
  • unmark

What endpoint should I use for that? /quote/ID/share seems to be a terrible idea. I have thought of a POST to /quote/ID with an "action" parameter that tells the script what action to perform on the quote, would that be correct?

1

There are 1 answers

7
kaaposc On BEST ANSWER

Try to separate API from your application logic. From API you GET the quote and API from now on shouldn't care what you do with that data. The same with marking quotes as favorites. It's your application's and user db's problem how to mark something. Again, API should care only about properly answering to you GETs.

Think about REST API as a database--you GET data from there, you can PUT or POST some data, but things like sharing or marking should be done in application.

Edit

About endpoints.

  • GET/POST/PUT /quote[s] should care only about quotes;
  • PUT/POST /user/{userId}/action with POST data like {"type": "share", "target": "otherUserId", "quoteId": 123} could be used for saving data about actions in DB.