RESTful URL for “Activate”

2019-04-08 09:41发布

问题:

I have a resource (project) which can be activated and deactivated.
What's the most RESTful URL endpoint for this purpose?

Right now I'm thinking about /projects/:id/activate and /projects/:id/deactivate, but I don't think that's very RESTful.
In addition, I'm not certain what HTTP method to use.

Can you provide some pointers?
Thanks!

回答1:

Your can send your requests just to projects/{id} and use PATCH (as you're updating existing object) verb, e.g.

PATCH /projects/123

[
    { "op": "activate|deactivate", ... }
]

Read more: REST API - PUT vs PATCH with real life examples



回答2:

The most conventional way to do this is via POST to /projects/:id, with parameters indicating whether you want to activate or deactivate or something else (always leave room for something else).

note that RESTful URLs should refer to things (like projects), not actions. Then the common methods have clear meanings:

  • PUT: create or replace the thing
  • PATCH: set properties of the thing
  • POST: perform an operation on the thing
  • GET: retrieve the thing
  • DELETE: delete the thing


标签: api rest http url