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
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.
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.