-2

I am implementing some REST APIs and I have a doubt about what HTTP verb should I use to implement an API that remove a single record from my DB

I thought that I should use the DELETE method but reading here: http://www.restapitutorial.com/lessons/httpmethods.html

Reading the DELETE verb defintion I can read:

405 (Method Not Allowed), unless you want to delete the whole collection—not often desirable.

So it seems to me that I have to use DELETE when I delete the whole collection and not a single element. Or it fit also the use case in which I am deleting a specific object (passing the ID of this object into my URL)?

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

1 Answers1

0

Looks like you have misunderstood the content of the link you posted.

The DELETE HTTP method can be used to remove a single resource or a collection of resources.

However, in most of situations, you don't want to allow the client to remove the entire collection, so you return a 405 status code indicating that the DELETE method is not allowed in the collection URL.


In more practical terms, consider that you have a collection of customers mapped to /customers.

A GET request to such URL will return a representation of this collection. A DELETE request to such URL will delete all customers from your application. It's very likely you don't want that, so you return 405 to indicate that deleting all customers at once is not allowed. In the other hand, you want to allow the client to delete a single customer by performing a DELETE request to a URL that locates a single customer, such as /customers/{id}.

If the request succeeds, consider one of the following status codes, as defined in the RFC 7231:

If a DELETE method is successfully applied, the origin server SHOULD send a 202 (Accepted) status code if the action will likely succeed but has not yet been enacted, a 204 (No Content) status code if the action has been enacted and no further information is to be supplied, or a 200 (OK) status code if the action has been enacted and the response message includes a representation describing the status.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359