Insomnia: DELETE

Before you write the DELETE method, save the request that deletes a category in Insomnia, with no body, decide what it should answer, and plan how to prove the category is gone.

Journeyman · Spring Boot · REST API · Updated

By

After this lesson you can

  • Send a DELETE request to a Spring Boot API from Insomnia and read 204 No Content

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.
  • This API answers 204 even when there was nothing to delete, so prove it's gone with a GET.

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 (10 here; use your own, as in Insomnia: PUT):

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

Leave the Body tab as it is, with no body. Run your app, and send it. The answer is 405 Method Not Allowed, and the Headers tab shows Allow: PUT: the address answers PUT since the last lesson, but not DELETE yet.

The answer you want

Once the next lesson is built, 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.

Prove it's gone

Send All categories: Snacks, category 10, is no longer in the list. That's the check that counts.

Send Delete Snacks a second time, though, and this API answers 204 again. Its delete method runs the SQL DELETE and doesn't look at whether a row was there, so deleting nothing looks the same as deleting something. Many APIs answer 404 Not Found instead: Returning ResponseEntity<T>, later in this sprint, shows how to choose the status code yourself.

Do it: prove a category is gone

Delete Snacks answers 204 whether or not there was anything to delete, so the status can't prove Snacks is gone. The check that counts is All categories: if no category in its answer has Snacks' id, it's gone. Write that check.

Category is given at the bottom of the file, with getCategoryId() and getCategoryName().

The method gets two values:

  • allCategories: the categories in the answer to All categories, a List<Category>.
  • id: the id you deleted, an int, such as 10.

For example, after Delete Snacks, All categories answers with Beverages (1), Condiments (2) and Seafood (8):

Category Its id is 10?
Beverages no: 1
Condiments no: 2
Seafood no: 8

No category has the id 10, so isGone(allCategories, 10) returns true.

Your job: write the method isGone(List<Category> allCategories, int id) so it returns a boolean.

  • Return false when any category in the list has that id: it's still there.
  • Return true when none does, including when the list is empty.

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

Check yourself

2 questions to try on the lesson page.

So what

One more to-do item, and a lesson in checking: a status code says what the API thinks it did, and a GET shows what's really there. In the next lesson you write the DELETE method, and your collection then holds a request for every method of the categories API.

Key terms

Cheat sheets