Saturday, May 4, 2019

How do I write API to delete a resource ?

Generally, we delete single resource at once so your route ends with an identifier of resource. Method you use to delete a resource is DELETE

DELETE /api/v1/departments/15

Basic rules:

  1. You must use HTTP DELETE method for deleting a resource.
  2. End point should be single (if not exceptional scenario e.g. Bulk delete)
  3. Proper http response codes must be returned
  4. Generally request has no body because identifier in route locates a resource to delete.

Http response codes to return

HTTP CodeWhen to return
200 - OKWhen api delete a complete resource successfully.
400 - Bad requestWhen api fails to invalid identifier is sent through request. e.g. if identifier is numeric and you pass negative value.
404 - Not foundWhen resource you are looking for does not exist

No comments:

Post a Comment