Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a RESTful API, should DELETE calls be recursive?

Say I have the following relational structure of data (for examples sake):

A `post` which is a child of a `category`

This data can be accessed via these API endpoints:

  • GET /category/1
  • GET /category/1/post/1
  • GET /category/1/post/2

I have now decided that I want to use my API to delete category 1 so I run DELETE /category/1.

However, category 1 has relational child post elements that can't exist without the category, would you expect this call to either fail and say you must first delete the child elements OR automatically recursively delete the child elements when you delete the category?

like image 305
Dan Avatar asked Jan 30 '18 11:01

Dan


People also ask

Which method is completely deleting in RESTful API?

In RESTful APIs resources are typically deleted using the HTTP DELETE method. The resource that should be deleted is identified by the request URI.

What should I return on Delete REST API?

A successful response of DELETE requests SHOULD be an HTTP response code 200 (OK) if the response includes an entity describing the status. The status should be 202 (Accepted) if the action has been queued.

How do I delete data from REST API?

Use the sObject Rows resource to delete records. Specify the record ID and use the DELETE method of the resource to delete a record.

How are REST API calls handled?

Under REST architecture, the client and server can only interact in one way: The client sends a request to the server, then the server sends a response back to the client. Servers cannot make requests and clients cannot respond — all interactions are initiated by the client.


1 Answers

IMO this is more of a design decision and their really is no wrong way to do it. So it all depends on the requirements.

If you decide that deleting a category deletes all the child post elements, then you can do that in multiple way (ordered by my preference).

  • Control that cascading deletes in the database.
  • Add code in the DAL layer so that when a category is called, it deletes all the post under the category.

If you decide to not do the "cascade" delete for the child post, then your only option is to return an appropriate error message stating why the category cannot be deleted.

If you want you can make it more clear what the call to the web service does by doing something like this.

DELETE /category/1?includePost=true --> Deletes category #1 & all post under it.

DELETE /category/1 --> Delete category #1 or returns error if it can't delete it.

like image 194
smehaffie Avatar answered Sep 21 '22 03:09

smehaffie