0

I am a backend developer and I am writing a restful application. I have a question about how best to design the endpoints.

I have a database table that stores values (I just provided an example)

|id (int)|A (bool)| B (bool)|
| 1      |   0    |    0    |
| 2      |   0    |    1    |
| 3      |   1    |    1    |

Business logic:

  1. If A=0 B=0 then we consider "no action"
  2. If A=0 B=1 then we consider that "the action has begun"
  3. If A=1 B=1 then we consider "action ended"

How can I do it better?

Style 1

Make one endpoint [PUT] /action to the body of which I will expect from the frontender

{"id": int, "A": bool, "B": bool}

In this case, I'm shifting to the front-end the writing of the logic for separating the use cases of the endpoint

Style 2

Make a separate endpoint for each business function

  1. [POST] /action/begin
  2. [POST] /action/end
  3. [DELETE] /action

In this case I expect only the id of the object from the front, and I will fill in A and B myself

4
  • 1
    Your database table (and it's corresponding JSON) are implementation details. There is some benefit to hiding those details via Style 2. Aug 31, 2022 at 14:29
  • But there is no "best" way. There is only that way that best meets your specific needs. Aug 31, 2022 at 14:29
  • 2
    The semantics of your application should be reflected in the api, but with this foo bar example there are no semantics to inform the style
    – Ewan
    Aug 31, 2022 at 14:32
  • It's hard to opine without knowing the specifics but it seems to me that Style 2 is almost intuitively obvious while Style 1 is completely obscure and would require documentation to understand. So my instinct is to prefer Style 2. Style 2 also lets you use HTTP nodes (e.g. firewall, proxy) to filter, restrict, or route requests by path or verb. With Style 1 it would not be very natural to do the same.
    – John Wu
    Sep 2, 2022 at 16:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.