0
app.delete('/poems/delete:ids',(req,res,next)=>{
    //here req.params.ids carries the multiple id i.e 1,2,3

});

In the above code, how can I delete all data with the requested ids that is the data's containing ids 1,2,3

 this.http.delete('http://localhost:3000/poems/delete' + ['1','3','4']);
3
  • 2 things - why use the DELETE verb with a CRUD URL? Also, if ids are arbitrary then you'd be best using POST and passing the IDs up in the body given URLs have a maximum length. Commented Mar 8, 2019 at 14:52
  • ok, thank u so much. Commented Mar 8, 2019 at 15:01
  • can u please, why req.params.ids stores the values in the format like: 1,2,3 instead of [1,2,3] . Commented Mar 8, 2019 at 15:05

2 Answers 2

1

The best practice in this case is to submit a POST request, whose body is an array of IDs. In that case, your controller would get req.body.ids and delete them accordingly.

The DELETE HTTP Method is used for single deletions.

Sign up to request clarification or add additional context in comments.

Comments

0

I faced the same issue.

For deleting multiple ids, it's better to use DELETE since it's the more semantically correct method for resource deletion. However, DELETE has some limitations:

  • DELETE does not support a request body in some browsers and HTTP clients (though in practice, most servers allow it).
  • POST is more flexible and universally supported, but it is semantically meant for creating or processing data, not deleting it.

Possible Approaches which I find:

  1. Use DELETE with query parameters:

DELETE /users?ids=1,2,3,4

The server retrieves ids from the query parameters and deletes them.

  1. Use DELETE with a request body (if supported by the server):
DELETE /items
Content-Type: application/json

{
    "ids": [1, 2, 3, 4]
}
  1. Use POST (if DELETE with a request body is not supported):

This approach is more compatible but less RESTful.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.