Insomnia: DELETE

Before you build the DELETE action, write the requests that delete a category in Insomnia, with no body, and decide what each should answer, including a second send of the same request and a category the database won't let go.

Journeyman · REST API · ASP.NET Core · Updated

After this lesson you can

  • Send a DELETE request from Insomnia and read 204 No Content and 404 Not Found

Key takeaways

  • A DELETE goes to one item's address and has no body. The id in the address says which item.
  • The answer you want is 204 No Content, with an empty body. Send the same request again, and it should answer 404 Not Found, because the item is gone.
  • A category that still has products can't be deleted. The database refuses, and for now the API answers 500 Internal Server Error.

Your API can read, add and change categories. The last thing a client needs is to remove one. Snacks was only ever a test, so it's the one to delete. As before, write down what you want the API to answer first.

A request with no body

A DELETE goes to one item's address, like PUT, but it carries no body: the id in the address says everything. There's nothing to describe about an item you're throwing away.

Add a request to the Northwind API collection, name it Delete Snacks, and set it to Snacks' id (9 here; use your own, as in Insomnia: PUT):

DELETE {{ _.base_url }}/api/categories/9

Leave the Body tab as it is, with no body. Start your API, and send it. The answer is 405 Method Not Allowed, and the Headers tab shows Allow: GET, PUT: the address answers those two methods, but not DELETE yet.

The answers you want

Once the next lesson is built:

  1. The first send of Delete Snacks should answer 204 No Content. No Content isn't a problem: it means "done, and there's nothing to send back". The body is empty, because the category is gone.
  2. The second send of the same request should answer 404 Not Found. Category 9 is already deleted, so there's nothing at that address to delete.
  3. Category 9, your GET from Insomnia: PUT, should then answer 404 Not Found too.

Seeing the second send fail is part of checking that the first one worked.

A category the database won't let go

Add one more request, Delete Beverages:

DELETE {{ _.base_url }}/api/categories/1

Beverages has products: the Products table points at it. Deleting it would leave those products pointing at nothing, so the database refuses, as you saw in Database Design in SQL Server. After the next lesson, this request answers 500 Internal Server Error, which is the server saying "something broke". That's not the answer you want either: nothing broke, the request just isn't allowed. Keep the request in your collection. Handling Global Exceptions in ASP.NET Core, later in this chapter, makes it answer properly.

Don't worry about losing Beverages while you test: the database's rule keeps it safe.

Do it: predict what each DELETE answers

Delete Snacks answers differently the second time you send it, and Delete Beverages is turned away. Write a method that works out the answer to each send, in order, so you know what to expect once the action is built.

The method gets three values:

  • categoryIds: the ids of the categories saved now, a List<int>.
  • withProducts: the ids of the categories that still have products, a List<int>. The database won't delete these.
  • sends: the id in each DELETE you send, in the order you send them, a List<int>.

For example, categories 1, 2 and 9 are saved, 1 and 2 have products, and you send DELETE /api/categories/9 twice, then DELETE /api/categories/1:

Send Is the id saved? Does it have products? Answer
9 yes no 204, and 9 is deleted
9 no: it's gone 404
1 yes yes 500: the database refuses, and 1 stays

So Answers(new List<int> { 1, 2, 9 }, new List<int> { 1, 2 }, new List<int> { 9, 9, 1 }) returns a list of 204, 404 and 500.

Your job: write the method Answers(List<int> categoryIds, List<int> withProducts, List<int> sends) so it returns the status code of each send, in order, a List<int>.

  • When the id isn't saved, the answer is 404.
  • When the id is saved and has products, the answer is 500, and the id stays saved.
  • Otherwise, the answer is 204, and the id is no longer saved: a later send of it answers 404.
  • With no sends, return an empty list.

Practiced live on the lesson page: write the C# right there and check it.

Check yourself

2 questions to try on the lesson page.

So what

Three to-do items: delete Snacks, prove it's gone, and see what happens when the database says no. In the next lesson you build the DELETE action, and your collection then holds a request for every endpoint of the categories API.

Key terms

Cheat sheets